<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="pandas," />





  <link rel="alternate" href="/blog/atom.xml" title="稻草人的编程之路" type="application/atom+xml" />






<meta name="description" content="pandaspandas是一个开源的、高性能、易于使用的用于数据结构化与分析python第三方库。本文是涉及比较常见的padans操作，详情清参考pandas官网文档 数据准备为了便于练习，本文会以泰坦尼克号数据为例  titanic_train.csv 密码: hrmw  基础pandas 基于两种数据类型：Series与DataFrame 12Series: 一个一维的数据类型，其中每一个元素">
<meta name="keywords" content="pandas">
<meta property="og:type" content="article">
<meta property="og:title" content="数据分析之pandas">
<meta property="og:url" content="https://wangxiaochuang.github.io/2018/09/15/2.html">
<meta property="og:site_name" content="稻草人的编程之路">
<meta property="og:description" content="pandaspandas是一个开源的、高性能、易于使用的用于数据结构化与分析python第三方库。本文是涉及比较常见的padans操作，详情清参考pandas官网文档 数据准备为了便于练习，本文会以泰坦尼克号数据为例  titanic_train.csv 密码: hrmw  基础pandas 基于两种数据类型：Series与DataFrame 12Series: 一个一维的数据类型，其中每一个元素">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-01-07T22:36:29.352Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="数据分析之pandas">
<meta name="twitter:description" content="pandaspandas是一个开源的、高性能、易于使用的用于数据结构化与分析python第三方库。本文是涉及比较常见的padans操作，详情清参考pandas官网文档 数据准备为了便于练习，本文会以泰坦尼克号数据为例  titanic_train.csv 密码: hrmw  基础pandas 基于两种数据类型：Series与DataFrame 12Series: 一个一维的数据类型，其中每一个元素">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://wangxiaochuang.github.io/2018/09/15/2.html"/>





  <title>数据分析之pandas | 稻草人的编程之路</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">稻草人的编程之路</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">众人皆醉我独醒 举世皆浊我独清</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://wangxiaochuang.github.io/blog/2018/09/15/2.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jackstraw">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="稻草人的编程之路">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">数据分析之pandas</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-09-15T11:55:45+00:00">
                2018-09-15
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/数据分析/" itemprop="url" rel="index">
                    <span itemprop="name">数据分析</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/数据分析/基础工具/" itemprop="url" rel="index">
                    <span itemprop="name">基础工具</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2018/09/15/2.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/blog/2018/09/15/2.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/blog/2018/09/15/2.html" class="leancloud_visitors" data-flag-title="数据分析之pandas">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="pandas"><a href="#pandas" class="headerlink" title="pandas"></a>pandas</h1><p>pandas是一个开源的、高性能、易于使用的用于数据结构化与分析python第三方库。本文是涉及比较常见的padans操作，详情清参考<a href="http://pandas.pydata.org/pandas-docs/stable/">pandas官网文档</a></p>
<h2 id="数据准备"><a href="#数据准备" class="headerlink" title="数据准备"></a>数据准备</h2><p>为了便于练习，本文会以泰坦尼克号数据为例</p>
<ol>
<li><a href="https://pan.baidu.com/s/1TRZr824a7tVcpF1hSOlr5A">titanic_train.csv</a> 密码: hrmw</li>
</ol>
<h2 id="基础"><a href="#基础" class="headerlink" title="基础"></a>基础</h2><p>pandas 基于两种数据类型：Series与DataFrame</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Series: 一个一维的数据类型，其中每一个元素都有一个标签（即索引，可以是数字或字符串）</span><br><span class="line">DataFrame: 一个二维的表结构。DataFrame可以存储许多种不同的数据类型，且每一个坐标轴都有自己的标签</span><br></pre></td></tr></table></figure>
<h3 id="关于DataFrame的基本操作"><a href="#关于DataFrame的基本操作" class="headerlink" title="关于DataFrame的基本操作"></a>关于DataFrame的基本操作</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = &#123;</span><br><span class="line"><span class="meta">... </span>    <span class="string">'country'</span>: [<span class="string">'beijing'</span>, <span class="string">'chengdu'</span>, <span class="string">'shanghai'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'population'</span>: [<span class="number">10</span>, <span class="number">12</span>, <span class="number">14</span>]</span><br><span class="line"><span class="meta">... </span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(data)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">    country  population</span><br><span class="line"><span class="number">0</span>   beijing          <span class="number">10</span></span><br><span class="line"><span class="number">1</span>   chengdu          <span class="number">12</span></span><br><span class="line"><span class="number">2</span>  shanghai          <span class="number">14</span></span><br></pre></td></tr></table></figure>
<p>可以看到，构造一个DataFrame需要指定一个字典，字典的键表示列名，字典的值表示列数据，DataFrame有一些基础的属性与方法供我们使用。</p>
<div><div class="fold_hider"><div class="close hider_title">常用属性与方法实例</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 我们就简单看一下数据的结构，可是使用head/tail函数，参数表示显示的条目数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.head(<span class="number">1</span>)</span><br><span class="line">   country  population</span><br><span class="line"><span class="number">0</span>  beijing          <span class="number">10</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.tail(<span class="number">1</span>)</span><br><span class="line">    country  population</span><br><span class="line"><span class="number">2</span>  shanghai          <span class="number">14</span></span><br><span class="line"><span class="comment"># 我们也可以通过info函数粗略看一下这个数据的各类情况，比如：索引的范围、列的情况、数据类型以及占用的内存大小等</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.info()</span><br><span class="line">&lt;<span class="class"><span class="keyword">class</span> '<span class="title">pandas</span>.<span class="title">core</span>.<span class="title">frame</span>.<span class="title">DataFrame</span>'&gt;</span></span><br><span class="line"><span class="class"><span class="title">RangeIndex</span>:</span> <span class="number">3</span> entries, <span class="number">0</span> to <span class="number">2</span></span><br><span class="line">Data columns (total <span class="number">2</span> columns):</span><br><span class="line">country       <span class="number">3</span> non-null object</span><br><span class="line">population    <span class="number">3</span> non-null int64</span><br><span class="line">dtypes: int64(<span class="number">1</span>), object(<span class="number">1</span>)</span><br><span class="line">memory usage: <span class="number">128.0</span>+ bytes</span><br><span class="line"><span class="comment"># 我们可以获取DataFrame的索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.index</span><br><span class="line">RangeIndex(start=<span class="number">0</span>, stop=<span class="number">3</span>, step=<span class="number">1</span>)</span><br><span class="line"><span class="comment"># 获取DataFrame的所有列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.columns</span><br><span class="line">Index([<span class="string">'country'</span>, <span class="string">'population'</span>], dtype=<span class="string">'object'</span>)</span><br><span class="line"><span class="comment"># 获取ndarray的数据</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.values</span><br><span class="line">array([[<span class="string">'beijing'</span>, <span class="number">10</span>],</span><br><span class="line">       [<span class="string">'chengdu'</span>, <span class="number">12</span>],</span><br><span class="line">       [<span class="string">'shanghai'</span>, <span class="number">14</span>]], dtype=object)</span><br><span class="line"><span class="comment"># 获取每一个列的数据类型</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.dtypes</span><br><span class="line">country       object</span><br><span class="line">population     int64</span><br><span class="line">dtype: object</span><br><span class="line"><span class="comment"># 快速获取各类指标</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.describe()</span><br><span class="line">       population</span><br><span class="line">count         <span class="number">3.0</span></span><br><span class="line">mean         <span class="number">12.0</span></span><br><span class="line">std           <span class="number">2.0</span></span><br><span class="line">min          <span class="number">10.0</span></span><br><span class="line"><span class="number">25</span>%          <span class="number">11.0</span></span><br><span class="line"><span class="number">50</span>%          <span class="number">12.0</span></span><br><span class="line"><span class="number">75</span>%          <span class="number">13.0</span></span><br><span class="line">max          <span class="number">14.0</span></span><br></pre></td></tr></table></figure>

</div></div>
<p>读取<code>titanic_train.csv</code>文件，转化为DataFrame</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 直接使用read_csv函数读取csv文件即可转换为DataFrame，可以根据上面试验的函数与属性自行试验一下，这个数据集的基本信息</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">'titanic_train.csv'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.head(<span class="number">2</span>)</span><br><span class="line">   PassengerId  Survived  Pclass                                               Name     Sex   Age  SibSp  Parch     Ticket     Fare Cabin Embarked</span><br><span class="line"><span class="number">0</span>            <span class="number">1</span>         <span class="number">0</span>       <span class="number">3</span>                            Braund, Mr. Owen Harris    male  <span class="number">22.0</span>      <span class="number">1</span>      <span class="number">0</span>  A/<span class="number">5</span> <span class="number">21171</span>   <span class="number">7.2500</span>   NaN        S</span><br><span class="line"><span class="number">1</span>            <span class="number">2</span>         <span class="number">1</span>       <span class="number">1</span>  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  <span class="number">38.0</span>      <span class="number">1</span>      <span class="number">0</span>   PC <span class="number">17599</span>  <span class="number">71.2833</span>   C85        C</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">访问DataFrame示例</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 可以获取DataFrame中的一列，可以看到获取的这一列是一个Series结构，Series结构的讲解参见下文。该结构很多属性与DataFrame是类似的</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age = df[<span class="string">'Age'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age.head(<span class="number">2</span>)</span><br><span class="line"><span class="number">0</span>    <span class="number">22.0</span></span><br><span class="line"><span class="number">1</span>    <span class="number">38.0</span></span><br><span class="line">Name: Age, dtype: float64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>type(age)</span><br><span class="line">&lt;<span class="class"><span class="keyword">class</span> '<span class="title">pandas</span>.<span class="title">core</span>.<span class="title">series</span>.<span class="title">Series</span>'&gt;</span></span><br><span class="line"><span class="class"># 我们也可以按索引来访问，可以发现这种访问方式或出错，获取某个列的时候，我们是通过 <span class="title">df</span>['<span class="title">Age</span>']获取的么，可以想象这个位置应该是填写列名才对，要不然不是乱套了嘛</span></span><br><span class="line"><span class="class">&gt;&gt;&gt; <span class="title">df</span>[100]         # 错误的方式</span></span><br><span class="line"><span class="class">出错啦</span></span><br><span class="line"><span class="class"># 这种方式返回的也是一个<span class="title">Series</span>结构，<span class="title">Series</span>结构可以表示<span class="title">DataFrame</span>的一行或一列</span></span><br><span class="line"><span class="class">&gt;&gt;&gt; <span class="title">df</span>.<span class="title">iloc</span>[100]</span></span><br><span class="line"><span class="class"><span class="title">PassengerId</span>                        101</span></span><br><span class="line"><span class="class"><span class="title">Survived</span>                             0</span></span><br><span class="line"><span class="class"><span class="title">Pclass</span>                               3</span></span><br><span class="line">Name           Petranec, Miss. Matilda</span><br><span class="line">Sex                             female</span><br><span class="line">Age                                 <span class="number">28</span></span><br><span class="line">SibSp                                <span class="number">0</span></span><br><span class="line">Parch                                <span class="number">0</span></span><br><span class="line">Ticket                          <span class="number">349245</span></span><br><span class="line">Fare                            <span class="number">7.8958</span></span><br><span class="line">Cabin                              NaN</span><br><span class="line">Embarked                             S</span><br><span class="line">Name: <span class="number">100</span>, dtype: object</span><br><span class="line"><span class="comment"># 访问索引为1，列索引为3的数据，可以看到正式Name列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.iloc[<span class="number">1</span>, <span class="number">3</span>]</span><br><span class="line"><span class="string">'Cumings, Mrs. John Bradley (Florence Briggs Thayer)'</span></span><br><span class="line"><span class="comment"># 这两个位置的索引依然支持切片</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.iloc[<span class="number">0</span>:<span class="number">3</span>, <span class="number">2</span>:<span class="number">5</span>]</span><br><span class="line">   Pclass                                               Name     Sex</span><br><span class="line"><span class="number">0</span>       <span class="number">3</span>                            Braund, Mr. Owen Harris    male</span><br><span class="line"><span class="number">1</span>       <span class="number">1</span>  Cumings, Mrs. John Bradley (Florence Briggs Th...  female</span><br><span class="line"><span class="number">2</span>       <span class="number">3</span>                             Heikkinen, Miss. Laina  female</span><br><span class="line"><span class="comment"># 只想选第二列与第四列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.iloc[<span class="number">0</span>:<span class="number">3</span>, [<span class="number">2</span>,<span class="number">4</span>]]</span><br><span class="line">   Pclass     Sex</span><br><span class="line"><span class="number">0</span>       <span class="number">3</span>    male</span><br><span class="line"><span class="number">1</span>       <span class="number">1</span>  female</span><br><span class="line"><span class="number">2</span>       <span class="number">3</span>  female</span><br><span class="line"><span class="comment"># 有时使用列名来选择可能更加直观一些</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="number">0</span>:<span class="number">3</span>][[<span class="string">'Age'</span>, <span class="string">'Fare'</span>]]</span><br><span class="line">    Age     Fare</span><br><span class="line"><span class="number">0</span>  <span class="number">22.0</span>   <span class="number">7.2500</span></span><br><span class="line"><span class="number">1</span>  <span class="number">38.0</span>  <span class="number">71.2833</span></span><br><span class="line"><span class="number">2</span>  <span class="number">26.0</span>   <span class="number">7.9250</span></span><br><span class="line"><span class="comment"># 不指定行索引，按列名选择数据</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[[<span class="string">'Age'</span>, <span class="string">'Fare'</span>]]</span><br><span class="line">内容太多啦，不贴啦</span><br><span class="line"><span class="comment"># 可以看到通过这种方式选择出来的是一个DataFrame，能看出与 df['Age'] 的区别么</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>type(df[[<span class="string">'Age'</span>, <span class="string">'Fare'</span>]])</span><br><span class="line">&lt;<span class="class"><span class="keyword">class</span> '<span class="title">pandas</span>.<span class="title">core</span>.<span class="title">frame</span>.<span class="title">DataFrame</span>'&gt;</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"># 目前位置我们讨论的索引都是系统默认生成的序号，其实我们是可以按指定列做为索引的，在时间处理中尤为有用</span></span><br><span class="line">&gt;&gt;&gt; newdf = df.set_index('Name')</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newdf.loc[<span class="string">'Heikkinen, Miss. Laina'</span>]</span><br><span class="line">PassengerId                   <span class="number">3</span></span><br><span class="line">Survived                      <span class="number">1</span></span><br><span class="line">Pclass                        <span class="number">3</span></span><br><span class="line">Sex                      female</span><br><span class="line">Age                          <span class="number">26</span></span><br><span class="line">SibSp                         <span class="number">0</span></span><br><span class="line">Parch                         <span class="number">0</span></span><br><span class="line">Ticket         STON/O2. <span class="number">3101282</span></span><br><span class="line">Fare                      <span class="number">7.925</span></span><br><span class="line">Cabin                       NaN</span><br><span class="line">Embarked                      S</span><br><span class="line">Name: Heikkinen, Miss. Laina, dtype: object</span><br><span class="line"><span class="comment"># 或者取这个人的年龄</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newdf.loc[<span class="string">'Heikkinen, Miss. Laina'</span>, <span class="string">'Age'</span>]</span><br><span class="line"><span class="number">26.0</span></span><br><span class="line"><span class="comment"># 取这个人的多个指标</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newdf.loc[<span class="string">'Heikkinen, Miss. Laina'</span>, [<span class="string">'Age'</span>, <span class="string">'Sex'</span>]]</span><br><span class="line">Age        <span class="number">26</span></span><br><span class="line">Sex    female</span><br><span class="line">Name: Heikkinen, Miss. Laina, dtype: object</span><br><span class="line"><span class="comment"># 甚至姓名也能使用切片，后面会讲时间相关的，会看到其强大的用途</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newdf.loc[<span class="string">'Heikkinen, Miss. Laina'</span>:<span class="string">'Allen, Mr. William Henry'</span>, [<span class="string">'Age'</span>, <span class="string">'Sex'</span>]]</span><br><span class="line">                                               Age     Sex</span><br><span class="line">Name</span><br><span class="line">Heikkinen, Miss. Laina                        <span class="number">26.0</span>  female</span><br><span class="line">Futrelle, Mrs. Jacques Heath (Lily May Peel)  <span class="number">35.0</span>  female</span><br><span class="line">Allen, Mr. William Henry                      <span class="number">35.0</span>    male</span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="关于Series的基本操作"><a href="#关于Series的基本操作" class="headerlink" title="关于Series的基本操作"></a>关于Series的基本操作</h3><p>Series类似ndarray结构是通过数组创建<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series(data=data, index=index)</span><br><span class="line"><span class="comment"># 可以看到Series并没列名信息，因为就一列，第一列是索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">a    <span class="number">10</span></span><br><span class="line">b    <span class="number">11</span></span><br><span class="line">c    <span class="number">12</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 我们依然能用head/tail方法</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.head(<span class="number">1</span>)</span><br><span class="line">a    <span class="number">10</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.tail(<span class="number">1</span>)</span><br><span class="line">c    <span class="number">12</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 获取ndarray数组</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.values</span><br><span class="line">array([<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>])</span><br><span class="line"><span class="comment"># 也能获取元素类型</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.dtypes</span><br><span class="line">dtype(<span class="string">'int64'</span>)</span><br><span class="line"><span class="comment"># 获取Series数据集中不重复的元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(&#123;</span><br><span class="line"><span class="meta">... </span>    <span class="string">'product'</span>: [<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'amount'</span>: [<span class="number">0</span>, <span class="number">5</span>, <span class="number">10</span>, <span class="number">5</span>, <span class="number">10</span>, <span class="number">15</span>, <span class="number">10</span>, <span class="number">15</span>, <span class="number">20</span>]</span><br><span class="line"><span class="meta">... </span>&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'product'</span>].unique()</span><br><span class="line">array([<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>], dtype=object)</span><br></pre></td></tr></table></figure></p>
<p>Series结构也支持一系列的数值运算</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">'titanic_train.csv'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age = df[<span class="string">'Age'</span>]</span><br><span class="line"><span class="comment"># 我们计算平均年龄</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age.mean()</span><br><span class="line"><span class="number">29.69911764705882</span></span><br><span class="line"><span class="comment"># 以及最大最小值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age.min()</span><br><span class="line"><span class="number">0.42</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age.max()</span><br><span class="line"><span class="number">80.0</span></span><br><span class="line"><span class="comment"># 其他等等</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age.std()</span><br><span class="line"><span class="number">14.526497332334044</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age.var()</span><br><span class="line"><span class="number">211.0191247463081</span></span><br><span class="line"><span class="comment"># 通过describe查看各种统计数据</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age.describe()</span><br><span class="line">count    <span class="number">714.000000</span></span><br><span class="line">mean      <span class="number">29.699118</span></span><br><span class="line">std       <span class="number">14.526497</span></span><br><span class="line">min        <span class="number">0.420000</span></span><br><span class="line"><span class="number">25</span>%       <span class="number">20.125000</span></span><br><span class="line"><span class="number">50</span>%       <span class="number">28.000000</span></span><br><span class="line"><span class="number">75</span>%       <span class="number">38.000000</span></span><br><span class="line">max       <span class="number">80.000000</span></span><br><span class="line">Name: Age, dtype: float64</span><br></pre></td></tr></table></figure>
<h2 id="进阶"><a href="#进阶" class="headerlink" title="进阶"></a>进阶</h2><p>基础篇的例子都看懂了，我们就可以进行高级一点的内容了</p>
<h3 id="数据的筛选"><a href="#数据的筛选" class="headerlink" title="数据的筛选"></a>数据的筛选</h3><p>基础篇我们介绍了如果去获取数据，这里我们讲讲如何去筛选各种数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 筛选出年龄大于73岁的人</span></span><br><span class="line"><span class="comment"># 这里的flag是一个Series结构，索引就是原始df的索引，列数据为bool类型的值，指明了年龄是否是大于74岁的</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>flag = df[<span class="string">'Age'</span>] &gt; <span class="number">73</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[flag]    <span class="comment"># 等价于 df.loc[flag]</span></span><br><span class="line">     PassengerId  Survived  Pclass                                  Name   Sex   Age  SibSp  Parch  Ticket    Fare Cabin Embarked</span><br><span class="line"><span class="number">630</span>          <span class="number">631</span>         <span class="number">1</span>       <span class="number">1</span>  Barkworth, Mr. Algernon Henry Wilson  male  <span class="number">80.0</span>      <span class="number">0</span>      <span class="number">0</span>   <span class="number">27042</span>  <span class="number">30.000</span>   A23        S</span><br><span class="line"><span class="number">851</span>          <span class="number">852</span>         <span class="number">0</span>       <span class="number">3</span>                   Svensson, Mr. Johan  male  <span class="number">74.0</span>      <span class="number">0</span>      <span class="number">0</span>  <span class="number">347060</span>   <span class="number">7.775</span>   NaN        S</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选择性别都是男性的记录</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[df[<span class="string">'Sex'</span>] == <span class="string">'male'</span>].head(<span class="number">1</span>)</span><br><span class="line">   PassengerId  Survived  Pclass                     Name   Sex   Age  SibSp  Parch     Ticket  Fare Cabin Embarked</span><br><span class="line"><span class="number">0</span>            <span class="number">1</span>         <span class="number">0</span>       <span class="number">3</span>  Braund, Mr. Owen Harris  male  <span class="number">22.0</span>      <span class="number">1</span>      <span class="number">0</span>  A/<span class="number">5</span> <span class="number">21171</span>  <span class="number">7.25</span>   NaN        S</span><br><span class="line"></span><br><span class="line"><span class="comment"># 结合我们在基础篇讲的获取数据的内容，这里求所有男性的平均年龄</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[df[<span class="string">'Sex'</span>] == <span class="string">'male'</span>, <span class="string">'Age'</span>].mean()</span><br><span class="line"><span class="number">30.72664459161148</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 求年龄大于70的人数</span></span><br><span class="line">(df[<span class="string">'Age'</span>] &gt; <span class="number">70</span>).sum()          </span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>(df[<span class="string">'Age'</span>] &gt; <span class="number">70</span>).sum()</span><br><span class="line"><span class="number">5</span></span><br><span class="line"><span class="comment"># 是否有考虑过这种求法，想一下为什么是这个结果</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[df[<span class="string">'Age'</span>] &gt; <span class="number">70</span>].sum()</span><br><span class="line">PassengerId                                                 <span class="number">2191</span></span><br><span class="line">Survived                                                       <span class="number">1</span></span><br><span class="line">Pclass                                                         <span class="number">9</span></span><br><span class="line">Name           Goldschmidt, Mr. George BConnors, Mr. PatrickA...</span><br><span class="line">Sex                                         malemalemalemalemale</span><br><span class="line">Age                                                        <span class="number">366.5</span></span><br><span class="line">SibSp                                                          <span class="number">0</span></span><br><span class="line">Parch                                                          <span class="number">0</span></span><br><span class="line">Ticket                         PC <span class="number">17754370369</span>PC <span class="number">1760927042347060</span></span><br><span class="line">Fare                                                     <span class="number">129.683</span></span><br><span class="line">Embarked                                                   CQCSS</span><br><span class="line">dtype: object</span><br></pre></td></tr></table></figure>
<h3 id="groupby"><a href="#groupby" class="headerlink" title="groupby"></a>groupby</h3><p>groupby也是一个高频的操作，理解了groupby将会非常有用</p>
<p>groupby: 将样本按照一定规则进行分组，然后得到分组后的统计信息，这里我们举一个简单的销售额的例子，假设有如下数据</p>
<div><div class="fold_hider"><div class="close hider_title">groupby示例</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 这里模拟了拥有三个产品（A、B、C）的销售额数据</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(&#123;</span><br><span class="line"><span class="meta">... </span>    <span class="string">'product'</span>: [<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'amount'</span>: [<span class="number">0</span>, <span class="number">5</span>, <span class="number">10</span>, <span class="number">5</span>, <span class="number">10</span>, <span class="number">15</span>, <span class="number">10</span>, <span class="number">15</span>, <span class="number">20</span>]</span><br><span class="line"><span class="meta">... </span>&#125;)</span><br><span class="line"><span class="comment"># 如果不用groupby，直接计算</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> key <span class="keyword">in</span> df[<span class="string">'product'</span>].unique():</span><br><span class="line"><span class="meta">... </span>    print(key, df[df[<span class="string">'product'</span>] == key].sum())</span><br><span class="line">...</span><br><span class="line">A product    AAA</span><br><span class="line">amount      <span class="number">15</span></span><br><span class="line">dtype: object</span><br><span class="line">B product    BBB</span><br><span class="line">amount      <span class="number">30</span></span><br><span class="line">dtype: object</span><br><span class="line">C product    CCC</span><br><span class="line">amount      <span class="number">45</span></span><br><span class="line">dtype: object</span><br><span class="line"><span class="comment"># 如果使用groupby，直接返回一个DataFrame结构，清晰明了</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby(<span class="string">'product'</span>).sum()</span><br><span class="line">         amount</span><br><span class="line">product</span><br><span class="line">A            <span class="number">15</span></span><br><span class="line">B            <span class="number">30</span></span><br><span class="line">C            <span class="number">45</span></span><br></pre></td></tr></table></figure>

</div></div>
<p>这里介绍groupby的工作流程</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">                split    apply    combine</span><br><span class="line">A 0             A 0               </span><br><span class="line">B 5             A 5</span><br><span class="line">C 10            A 10</span><br><span class="line">A 5     分块     B 5      sum      A 15</span><br><span class="line">B 10   ====&gt;    B 10     ====&gt;    B 30</span><br><span class="line">C 15            B 15              C 45</span><br><span class="line">A 10            C 10</span><br><span class="line">B 15            C 15</span><br><span class="line">c 20            C 20</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">groupby也支持很多的其他聚合方法</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 获取各类指标</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby(<span class="string">'product'</span>).describe()</span><br><span class="line">        amount</span><br><span class="line">         count  mean  std   min   <span class="number">25</span>%   <span class="number">50</span>%   <span class="number">75</span>%   max</span><br><span class="line">product</span><br><span class="line">A          <span class="number">3.0</span>   <span class="number">5.0</span>  <span class="number">5.0</span>   <span class="number">0.0</span>   <span class="number">2.5</span>   <span class="number">5.0</span>   <span class="number">7.5</span>  <span class="number">10.0</span></span><br><span class="line">B          <span class="number">3.0</span>  <span class="number">10.0</span>  <span class="number">5.0</span>   <span class="number">5.0</span>   <span class="number">7.5</span>  <span class="number">10.0</span>  <span class="number">12.5</span>  <span class="number">15.0</span></span><br><span class="line">C          <span class="number">3.0</span>  <span class="number">15.0</span>  <span class="number">5.0</span>  <span class="number">10.0</span>  <span class="number">12.5</span>  <span class="number">15.0</span>  <span class="number">17.5</span>  <span class="number">20.0</span></span><br><span class="line"><span class="comment"># 其他</span></span><br><span class="line"><span class="comment"># df.groupby('product').min()</span></span><br><span class="line"><span class="comment"># df.groupby('product').max()</span></span><br><span class="line"><span class="comment"># df.groupby('product').mean()</span></span><br><span class="line"><span class="comment"># mean求值等价于调用aggregate方法，其他类似</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby(<span class="string">'product'</span>).aggregate(np.mean)</span><br><span class="line">         amount</span><br><span class="line">product</span><br><span class="line">A             <span class="number">5</span></span><br><span class="line">B            <span class="number">10</span></span><br><span class="line">C            <span class="number">15</span></span><br></pre></td></tr></table></figure>

</div></div>
<div><div class="fold_hider"><div class="close hider_title">使用groupby实战泰坦尼克号的数据</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">'titanic_train.csv'</span>)</span><br><span class="line"><span class="comment"># 按Sex列进行分组，然后肯定只有两个组，男性和女性，这是再求对应指标（如Age）或所有指标的各类聚合信息，这里是求平均年龄</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby(<span class="string">'Sex'</span>)[<span class="string">'Age'</span>].mean()</span><br><span class="line">Sex</span><br><span class="line">female    <span class="number">27.915709</span></span><br><span class="line">male      <span class="number">30.726645</span></span><br><span class="line">Name: Age, dtype: float64</span><br><span class="line"><span class="comment"># 分别计算男性与女性的获救情况</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby(<span class="string">'Sex'</span>)[<span class="string">'Survived'</span>].mean()</span><br><span class="line">Sex</span><br><span class="line">female    <span class="number">0.742038</span></span><br><span class="line">male      <span class="number">0.188908</span></span><br><span class="line">Name: Survived, dtype: float64</span><br></pre></td></tr></table></figure>

</div></div>
<p>上面我们的例子都是按某一个列进行分组的，其实我们是可以自定义，按多列进行分组</p>
<div><div class="fold_hider"><div class="close hider_title">自定义分组</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(&#123;<span class="string">'A'</span> : [<span class="string">'foo'</span>, <span class="string">'bar'</span>, <span class="string">'foo'</span>, <span class="string">'bar'</span>, <span class="string">'foo'</span>, <span class="string">'bar'</span>, <span class="string">'foo'</span>, <span class="string">'foo'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'B'</span> : [<span class="string">'one'</span>, <span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'three'</span>, <span class="string">'two'</span>, <span class="string">'two'</span>, <span class="string">'one'</span>, <span class="string">'three'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'C'</span> : np.random.randn(<span class="number">8</span>),</span><br><span class="line"><span class="meta">... </span>    <span class="string">'D'</span> : np.random.randn(<span class="number">8</span>)&#125;)</span><br><span class="line"><span class="comment"># 按A列进行分组，求计数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped = df.groupby(<span class="string">'A'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped.count()</span><br><span class="line">     B  C  D</span><br><span class="line">A</span><br><span class="line">bar  <span class="number">3</span>  <span class="number">3</span>  <span class="number">3</span></span><br><span class="line">foo  <span class="number">5</span>  <span class="number">5</span>  <span class="number">5</span></span><br><span class="line"><span class="comment"># 分组后我们可以只求指定列的统计指标，比如这里获取C指标的总和、均值和标准差</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped[<span class="string">'C'</span>].agg([np.sum, np.mean, np.std])</span><br><span class="line">          sum      mean       std</span><br><span class="line">A</span><br><span class="line">bar <span class="number">-2.505948</span> <span class="number">-0.835316</span>  <span class="number">0.222418</span></span><br><span class="line">foo <span class="number">-0.880155</span> <span class="number">-0.176031</span>  <span class="number">0.626238</span></span><br><span class="line"><span class="comment"># 按A和B列进行分组，求计数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped = df.groupby([<span class="string">'A'</span>, <span class="string">'B'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped.count()</span><br><span class="line">           C  D</span><br><span class="line">A   B</span><br><span class="line">bar one    <span class="number">1</span>  <span class="number">1</span></span><br><span class="line">    three  <span class="number">1</span>  <span class="number">1</span></span><br><span class="line">    two    <span class="number">1</span>  <span class="number">1</span></span><br><span class="line">foo one    <span class="number">2</span>  <span class="number">2</span></span><br><span class="line">    three  <span class="number">1</span>  <span class="number">1</span></span><br><span class="line">    two    <span class="number">2</span>  <span class="number">2</span></span><br><span class="line"><span class="comment"># 我们也可以不要索引，通过as_index设置</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby([<span class="string">'A'</span>, <span class="string">'B'</span>],as_index=<span class="keyword">False</span>).count()</span><br><span class="line">     A      B  C  D</span><br><span class="line"><span class="number">0</span>  bar    one  <span class="number">1</span>  <span class="number">1</span></span><br><span class="line"><span class="number">1</span>  bar  three  <span class="number">1</span>  <span class="number">1</span></span><br><span class="line"><span class="number">2</span>  bar    two  <span class="number">1</span>  <span class="number">1</span></span><br><span class="line"><span class="number">3</span>  foo    one  <span class="number">2</span>  <span class="number">2</span></span><br><span class="line"><span class="number">4</span>  foo  three  <span class="number">1</span>  <span class="number">1</span></span><br><span class="line"><span class="number">5</span>  foo    two  <span class="number">2</span>  <span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped = df.groupby([<span class="string">'A'</span>, <span class="string">'B'</span>],as_index=<span class="keyword">False</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped.aggregate(np.sum)</span><br><span class="line">     A      B         C         D</span><br><span class="line"><span class="number">0</span>  bar    one <span class="number">-0.928055</span>  <span class="number">0.247315</span></span><br><span class="line"><span class="number">1</span>  bar  three <span class="number">-0.996357</span>  <span class="number">0.455334</span></span><br><span class="line"><span class="number">2</span>  bar    two <span class="number">-0.581536</span>  <span class="number">0.981273</span></span><br><span class="line"><span class="number">3</span>  foo    one <span class="number">-0.376836</span>  <span class="number">1.330245</span></span><br><span class="line"><span class="number">4</span>  foo  three  <span class="number">0.449950</span> <span class="number">-1.810325</span></span><br><span class="line"><span class="number">5</span>  foo    two <span class="number">-0.953268</span> <span class="number">-1.891668</span></span><br><span class="line"><span class="comment"># 与上例等价</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby([<span class="string">'A'</span>, <span class="string">'B'</span>]).sum().reset_index()</span><br><span class="line">与上面一致，不贴了</span><br><span class="line"><span class="comment"># 也能使用describe</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped = df.groupby([<span class="string">'A'</span>, <span class="string">'B'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped.describe()</span><br><span class="line">              C                                                                           D</span><br><span class="line">          count      mean       std       min       <span class="number">25</span>%       <span class="number">50</span>%       <span class="number">75</span>%       max count      mean       std       min       <span class="number">25</span>%       <span class="number">50</span>%       <span class="number">75</span>%       max</span><br><span class="line">A   B</span><br><span class="line">bar one     <span class="number">1.0</span> <span class="number">-0.928055</span>       NaN <span class="number">-0.928055</span> <span class="number">-0.928055</span> <span class="number">-0.928055</span> <span class="number">-0.928055</span> <span class="number">-0.928055</span>   <span class="number">1.0</span>  <span class="number">0.247315</span>       NaN  <span class="number">0.247315</span>  <span class="number">0.247315</span>  <span class="number">0.247315</span>  <span class="number">0.247315</span>  <span class="number">0.247315</span></span><br><span class="line">    three   <span class="number">1.0</span> <span class="number">-0.996357</span>       NaN <span class="number">-0.996357</span> <span class="number">-0.996357</span> <span class="number">-0.996357</span> <span class="number">-0.996357</span> <span class="number">-0.996357</span>   <span class="number">1.0</span>  <span class="number">0.455334</span>       NaN  <span class="number">0.455334</span>  <span class="number">0.455334</span>  <span class="number">0.455334</span>  <span class="number">0.455334</span>  <span class="number">0.455334</span></span><br><span class="line">    two     <span class="number">1.0</span> <span class="number">-0.581536</span>       NaN <span class="number">-0.581536</span> <span class="number">-0.581536</span> <span class="number">-0.581536</span> <span class="number">-0.581536</span> <span class="number">-0.581536</span>   <span class="number">1.0</span>  <span class="number">0.981273</span>       NaN  <span class="number">0.981273</span>  <span class="number">0.981273</span>  <span class="number">0.981273</span>  <span class="number">0.981273</span>  <span class="number">0.981273</span></span><br><span class="line">foo one     <span class="number">2.0</span> <span class="number">-0.188418</span>  <span class="number">0.644285</span> <span class="number">-0.643996</span> <span class="number">-0.416207</span> <span class="number">-0.188418</span>  <span class="number">0.039371</span>  <span class="number">0.267160</span>   <span class="number">2.0</span>  <span class="number">0.665123</span>  <span class="number">2.384508</span> <span class="number">-1.020979</span> <span class="number">-0.177928</span>  <span class="number">0.665123</span>  <span class="number">1.508173</span>  <span class="number">2.351224</span></span><br><span class="line">    three   <span class="number">1.0</span>  <span class="number">0.449950</span>       NaN  <span class="number">0.449950</span>  <span class="number">0.449950</span>  <span class="number">0.449950</span>  <span class="number">0.449950</span>  <span class="number">0.449950</span>   <span class="number">1.0</span> <span class="number">-1.810325</span>       NaN <span class="number">-1.810325</span> <span class="number">-1.810325</span> <span class="number">-1.810325</span> <span class="number">-1.810325</span> <span class="number">-1.810325</span></span><br><span class="line">    two     <span class="number">2.0</span> <span class="number">-0.476634</span>  <span class="number">0.762045</span> <span class="number">-1.015481</span> <span class="number">-0.746058</span> <span class="number">-0.476634</span> <span class="number">-0.207210</span>  <span class="number">0.062213</span>   <span class="number">2.0</span> <span class="number">-0.945834</span>  <span class="number">0.804517</span> <span class="number">-1.514713</span> <span class="number">-1.230274</span> <span class="number">-0.945834</span> <span class="number">-0.661394</span> <span class="number">-0.376954</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 除了使用列作为分组的条件外，我们也可以指定一个函数来设置分组的规则</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">get_letter_type</span><span class="params">(letter)</span>:</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">if</span> letter.lower() <span class="keyword">in</span> <span class="string">'aeiou'</span>:</span><br><span class="line"><span class="meta">... </span>            <span class="keyword">return</span> <span class="string">'a'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">else</span>:</span><br><span class="line"><span class="meta">... </span>            <span class="keyword">return</span> <span class="string">'b'</span></span><br><span class="line">...</span><br><span class="line"><span class="comment"># 制定 get_letter_type 函数为分组方式</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped = df.groupby(get_letter_type, axis=<span class="number">1</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped.count()</span><br><span class="line">   a  b</span><br><span class="line"><span class="number">0</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="number">1</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="number">2</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="number">3</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="number">4</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="number">5</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="number">6</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="number">7</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="comment"># 为了加深理解，我们在上面的例子基础上再给你一个例子，我们要按索引是否大于5位分组条件，来计算累计和</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">get_range_type</span><span class="params">(letter)</span>:</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">if</span> letter &gt; <span class="number">5</span>:</span><br><span class="line"><span class="meta">... </span>            <span class="keyword">return</span> <span class="string">'yes'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">else</span>:</span><br><span class="line"><span class="meta">... </span>            <span class="keyword">return</span> <span class="string">'no'</span></span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby(get_range_type, axis=<span class="number">0</span>).sum()</span><br><span class="line">            C         D</span><br><span class="line">no  <span class="number">-4.103212</span> <span class="number">-1.228725</span></span><br><span class="line">yes  <span class="number">0.717109</span>  <span class="number">0.540899</span></span><br></pre></td></tr></table></figure>

</div></div>
<p>分组后我们可能只想关注某一个分组<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(&#123;<span class="string">'X'</span>: [<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'A'</span>, <span class="string">'B'</span>], <span class="string">'Y'</span>: [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">   X  Y</span><br><span class="line"><span class="number">0</span>  A  <span class="number">1</span></span><br><span class="line"><span class="number">1</span>  B  <span class="number">2</span></span><br><span class="line"><span class="number">2</span>  A  <span class="number">3</span></span><br><span class="line"><span class="number">3</span>  B  <span class="number">4</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby(<span class="string">'X'</span>).get_group(<span class="string">'A'</span>)</span><br><span class="line">   X  Y</span><br><span class="line"><span class="number">0</span>  A  <span class="number">1</span></span><br><span class="line"><span class="number">2</span>  A  <span class="number">3</span></span><br></pre></td></tr></table></figure></p>
<p>多重索引下，我们可以制定level操作，level为0表示第一个索引，level为1表示第二个索引。。。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arrays = [[<span class="string">'bar'</span>, <span class="string">'bar'</span>, <span class="string">'baz'</span>, <span class="string">'baz'</span>, <span class="string">'foo'</span>, <span class="string">'foo'</span>, <span class="string">'qux'</span>, <span class="string">'qux'</span>],</span><br><span class="line"><span class="meta">... </span>    [<span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'one'</span>, <span class="string">'two'</span>]]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index = pd.MultiIndex.from_arrays(arrays, names=[<span class="string">'first'</span>, <span class="string">'second'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series(np.random.randn(<span class="number">8</span>),index=index)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">first  second</span><br><span class="line">bar    one       <span class="number">0.383993</span></span><br><span class="line">       two      <span class="number">-3.055530</span></span><br><span class="line">baz    one      <span class="number">-0.831237</span></span><br><span class="line">       two      <span class="number">-1.015493</span></span><br><span class="line">foo    one      <span class="number">-0.234695</span></span><br><span class="line">       two      <span class="number">-1.641438</span></span><br><span class="line">qux    one      <span class="number">-0.462693</span></span><br><span class="line">       two      <span class="number">-1.568615</span></span><br><span class="line">dtype: float64</span><br><span class="line"><span class="comment"># 我们按第一个索引进行求和</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.groupby(level=<span class="number">0</span>).sum()    <span class="comment"># 等价于 s.groupby(level="first").sum()</span></span><br><span class="line">first</span><br><span class="line">bar   <span class="number">-2.671537</span></span><br><span class="line">baz   <span class="number">-1.846730</span></span><br><span class="line">foo   <span class="number">-1.876133</span></span><br><span class="line">qux   <span class="number">-2.031308</span></span><br><span class="line">dtype: float64</span><br><span class="line"><span class="comment"># 也可以按第二个索引进行求和</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.groupby(level=<span class="number">1</span>).sum()    <span class="comment"># 等价于 s.groupby(level="second").sum()</span></span><br><span class="line">second</span><br><span class="line">one   <span class="number">-1.144633</span></span><br><span class="line">two   <span class="number">-7.281076</span></span><br><span class="line">dtype: float64</span><br></pre></td></tr></table></figure>
<h3 id="数值运算"><a href="#数值运算" class="headerlink" title="数值运算"></a>数值运算</h3><p>在讲解numpy那篇文章，我们讲解了其数值运算，pandas的数值运算同样强大</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 注意这里的index参数，默认我们的索引都是从0开始递增，有多少个记录索引就依次递增多少</span></span><br><span class="line"><span class="comment"># 除了使用默认的递增索引，我们也可以自己指定，还记得泰坦尼克号的数据，我们以名字作为索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame([[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],[<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]], index=[<span class="string">'a'</span>, <span class="string">'b'</span>], columns=[<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">   A  B  C</span><br><span class="line">a  <span class="number">1</span>  <span class="number">2</span>  <span class="number">3</span></span><br><span class="line">b  <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span></span><br><span class="line"><span class="comment"># 求和。对于DataFrame结构，其是一个table的结构，自然就有两个维度，横轴和纵轴，在求和的时候也需要注意是按哪个轴进行求和</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.sum()    <span class="comment"># 等价于 df.sum(axis=0) df.sum(axis='rows')</span></span><br><span class="line">A    <span class="number">5</span></span><br><span class="line">B    <span class="number">7</span></span><br><span class="line">C    <span class="number">9</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.sum(axis=<span class="number">1</span>)  <span class="comment"># 等价于 df.sum(axis='columns')</span></span><br><span class="line">a     <span class="number">6</span></span><br><span class="line">b    <span class="number">15</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 其他方法，同样需要指定轴</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.min()</span><br><span class="line">A    <span class="number">1</span></span><br><span class="line">B    <span class="number">2</span></span><br><span class="line">C    <span class="number">3</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.max()</span><br><span class="line">A    <span class="number">4</span></span><br><span class="line">B    <span class="number">5</span></span><br><span class="line">C    <span class="number">6</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.median()</span><br><span class="line">A    <span class="number">2.5</span></span><br><span class="line">B    <span class="number">3.5</span></span><br><span class="line">C    <span class="number">4.5</span></span><br><span class="line">dtype: float64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.mean()</span><br><span class="line">A    <span class="number">2.5</span></span><br><span class="line">B    <span class="number">3.5</span></span><br><span class="line">C    <span class="number">4.5</span></span><br><span class="line">dtype: float64</span><br><span class="line"><span class="comment"># 我们这里还是引入泰坦尼克号的数据</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">'titanic_train.csv'</span>)</span><br><span class="line"><span class="comment"># 我们要统计某一个指标的样本值的计数，默认是降序，可以通过 scending=True 指定为升序</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'Age'</span>].value_counts().head()</span><br><span class="line"><span class="number">24.0</span>    <span class="number">30</span></span><br><span class="line"><span class="number">22.0</span>    <span class="number">27</span></span><br><span class="line"><span class="number">18.0</span>    <span class="number">26</span></span><br><span class="line"><span class="number">19.0</span>    <span class="number">25</span></span><br><span class="line"><span class="number">30.0</span>    <span class="number">25</span></span><br><span class="line">Name: Age, dtype: int64</span><br><span class="line"><span class="comment"># 我们也可以指定区间范围，按范围计数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'Age'</span>].value_counts(bins=<span class="number">5</span>)</span><br><span class="line">(<span class="number">16.336</span>, <span class="number">32.252</span>]    <span class="number">346</span></span><br><span class="line">(<span class="number">32.252</span>, <span class="number">48.168</span>]    <span class="number">188</span></span><br><span class="line">(<span class="number">0.339</span>, <span class="number">16.336</span>]     <span class="number">100</span></span><br><span class="line">(<span class="number">48.168</span>, <span class="number">64.084</span>]     <span class="number">69</span></span><br><span class="line">(<span class="number">64.084</span>, <span class="number">80.0</span>]       <span class="number">11</span></span><br><span class="line">Name: Age, dtype: int64</span><br><span class="line"><span class="comment"># 再来一个好理解的，比如要统计所有人中男女的人数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'Sex'</span>].value_counts()</span><br><span class="line">male      <span class="number">577</span></span><br><span class="line">female    <span class="number">314</span></span><br><span class="line">Name: Sex, dtype: int64</span><br><span class="line"><span class="comment"># 如果要获取对应指标的样本数</span></span><br><span class="line"><span class="comment"># 本来这两个值都应该一样才对，但如果某一个指标存在缺失值，那么对应指标样本数就会变少</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'Age'</span>].count()</span><br><span class="line"><span class="number">714</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'Pclass'</span>].count()</span><br><span class="line"><span class="number">891</span></span><br></pre></td></tr></table></figure>
<p>除了单个指标的计算，我们也可以进行二元统计，比如计算<a href="https://www.cnblogs.com/tsingke/p/6273970.html">协方差</a>、<a href="https://www.cnblogs.com/sanshanyin/p/5397091.html">相关系数</a></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.cov()</span><br><span class="line">              PassengerId  Survived     Pclass         Age      SibSp     Parch         Fare</span><br><span class="line">PassengerId  <span class="number">66231.000000</span> <span class="number">-0.626966</span>  <span class="number">-7.561798</span>  <span class="number">138.696504</span> <span class="number">-16.325843</span> <span class="number">-0.342697</span>   <span class="number">161.883369</span></span><br><span class="line">Survived        <span class="number">-0.626966</span>  <span class="number">0.236772</span>  <span class="number">-0.137703</span>   <span class="number">-0.551296</span>  <span class="number">-0.018954</span>  <span class="number">0.032017</span>     <span class="number">6.221787</span></span><br><span class="line">Pclass          <span class="number">-7.561798</span> <span class="number">-0.137703</span>   <span class="number">0.699015</span>   <span class="number">-4.496004</span>   <span class="number">0.076599</span>  <span class="number">0.012429</span>   <span class="number">-22.830196</span></span><br><span class="line">Age            <span class="number">138.696504</span> <span class="number">-0.551296</span>  <span class="number">-4.496004</span>  <span class="number">211.019125</span>  <span class="number">-4.163334</span> <span class="number">-2.344191</span>    <span class="number">73.849030</span></span><br><span class="line">SibSp          <span class="number">-16.325843</span> <span class="number">-0.018954</span>   <span class="number">0.076599</span>   <span class="number">-4.163334</span>   <span class="number">1.216043</span>  <span class="number">0.368739</span>     <span class="number">8.748734</span></span><br><span class="line">Parch           <span class="number">-0.342697</span>  <span class="number">0.032017</span>   <span class="number">0.012429</span>   <span class="number">-2.344191</span>   <span class="number">0.368739</span>  <span class="number">0.649728</span>     <span class="number">8.661052</span></span><br><span class="line">Fare           <span class="number">161.883369</span>  <span class="number">6.221787</span> <span class="number">-22.830196</span>   <span class="number">73.849030</span>   <span class="number">8.748734</span>  <span class="number">8.661052</span>  <span class="number">2469.436846</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.corr()</span><br><span class="line">             PassengerId  Survived    Pclass       Age     SibSp     Parch      Fare</span><br><span class="line">PassengerId     <span class="number">1.000000</span> <span class="number">-0.005007</span> <span class="number">-0.035144</span>  <span class="number">0.036847</span> <span class="number">-0.057527</span> <span class="number">-0.001652</span>  <span class="number">0.012658</span></span><br><span class="line">Survived       <span class="number">-0.005007</span>  <span class="number">1.000000</span> <span class="number">-0.338481</span> <span class="number">-0.077221</span> <span class="number">-0.035322</span>  <span class="number">0.081629</span>  <span class="number">0.257307</span></span><br><span class="line">Pclass         <span class="number">-0.035144</span> <span class="number">-0.338481</span>  <span class="number">1.000000</span> <span class="number">-0.369226</span>  <span class="number">0.083081</span>  <span class="number">0.018443</span> <span class="number">-0.549500</span></span><br><span class="line">Age             <span class="number">0.036847</span> <span class="number">-0.077221</span> <span class="number">-0.369226</span>  <span class="number">1.000000</span> <span class="number">-0.308247</span> <span class="number">-0.189119</span>  <span class="number">0.096067</span></span><br><span class="line">SibSp          <span class="number">-0.057527</span> <span class="number">-0.035322</span>  <span class="number">0.083081</span> <span class="number">-0.308247</span>  <span class="number">1.000000</span>  <span class="number">0.414838</span>  <span class="number">0.159651</span></span><br><span class="line">Parch          <span class="number">-0.001652</span>  <span class="number">0.081629</span>  <span class="number">0.018443</span> <span class="number">-0.189119</span>  <span class="number">0.414838</span>  <span class="number">1.000000</span>  <span class="number">0.216225</span></span><br><span class="line">Fare            <span class="number">0.012658</span>  <span class="number">0.257307</span> <span class="number">-0.549500</span>  <span class="number">0.096067</span>  <span class="number">0.159651</span>  <span class="number">0.216225</span>  <span class="number">1.000000</span></span><br></pre></td></tr></table></figure>
<h3 id="DataFrame与Series对象的操作"><a href="#DataFrame与Series对象的操作" class="headerlink" title="DataFrame与Series对象的操作"></a>DataFrame与Series对象的操作</h3><p>前面讲了DataFrame和Series结构的一些常用操作，这里我们再讲一些其他的操作</p>
<div><div class="fold_hider"><div class="close hider_title">Series结构的其他常用操作</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 我们可以直接修改Series结构的值及索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = [<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series(data=data, index=index)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">a    <span class="number">10</span></span><br><span class="line">b    <span class="number">11</span></span><br><span class="line">c    <span class="number">12</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 拷贝一个DataFrame</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1 = s.copy()</span><br><span class="line"><span class="comment"># 这两种方式都可以定位到某一个元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1[<span class="string">'a'</span>] = <span class="number">100</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1[<span class="number">1</span>] = <span class="number">100</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1</span><br><span class="line">a    <span class="number">100</span></span><br><span class="line">b    <span class="number">100</span></span><br><span class="line">c     <span class="number">12</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 除了修改某一个元素，也可以修改某一堆元素，inplace表示知否修改原始数据</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1.replace(to_replace=<span class="number">100</span>, value=<span class="number">101</span>, inplace=<span class="keyword">True</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1</span><br><span class="line">a    <span class="number">101</span></span><br><span class="line">b    <span class="number">101</span></span><br><span class="line">c     <span class="number">12</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 除了修改值，也可以修改索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1.index = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'d'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1</span><br><span class="line">a    <span class="number">101</span></span><br><span class="line">b    <span class="number">101</span></span><br><span class="line">d     <span class="number">12</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 上面修改索引是全部修改，我们也可以只修改部分的索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1.rename(index=&#123;<span class="string">'a'</span>:<span class="string">'A'</span>, <span class="string">'b'</span>:<span class="string">'B'</span>&#125;, inplace=<span class="keyword">True</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1</span><br><span class="line">A    <span class="number">101</span></span><br><span class="line">B    <span class="number">101</span></span><br><span class="line">d     <span class="number">12</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 除了删除、修改，我们也可以直接增加</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s2 = pd.Series([<span class="number">100</span>,<span class="number">500</span>], index=[<span class="string">'g'</span>, <span class="string">'h'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s2</span><br><span class="line">g    <span class="number">100</span></span><br><span class="line">h    <span class="number">500</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 添加内容</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1.append(s2, ignore_index=<span class="keyword">False</span>)</span><br><span class="line">A    <span class="number">101</span></span><br><span class="line">B    <span class="number">101</span></span><br><span class="line">d     <span class="number">12</span></span><br><span class="line">g    <span class="number">100</span></span><br><span class="line">h    <span class="number">500</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 直接使用新的索引增加，就像使用字典一样</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1[<span class="string">'k'</span>] = <span class="number">110</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1</span><br><span class="line">A    <span class="number">101</span></span><br><span class="line">B    <span class="number">101</span></span><br><span class="line">d     <span class="number">12</span></span><br><span class="line">k    <span class="number">110</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 删除某一个记录</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> s1[<span class="string">'A'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1</span><br><span class="line">B    <span class="number">101</span></span><br><span class="line">d     <span class="number">12</span></span><br><span class="line">k    <span class="number">110</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 删除多行</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1.drop([<span class="string">'B'</span>, <span class="string">'d'</span>], inplace=<span class="keyword">True</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1</span><br><span class="line">k    <span class="number">110</span></span><br><span class="line">dtype: int64</span><br></pre></td></tr></table></figure>

</div></div>
<div><div class="fold_hider"><div class="close hider_title">DataFrame的常用操作</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = [[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],[<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index=[<span class="string">'a'</span>, <span class="string">'b'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>columns = [<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(data=data, index=index, columns=columns)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">   A  B  C</span><br><span class="line">a  <span class="number">1</span>  <span class="number">2</span>  <span class="number">3</span></span><br><span class="line">b  <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span></span><br><span class="line"><span class="comment"># 查询某一个指标</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'A'</span>]</span><br><span class="line">a    <span class="number">1</span></span><br><span class="line">b    <span class="number">4</span></span><br><span class="line">Name: A, dtype: int64</span><br><span class="line"><span class="comment"># 查询某一行</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.iloc[<span class="number">0</span>]      <span class="comment"># 等价于 df.loc['a']</span></span><br><span class="line">A    <span class="number">1</span></span><br><span class="line">B    <span class="number">2</span></span><br><span class="line">C    <span class="number">3</span></span><br><span class="line">Name: a, dtype: int64</span><br><span class="line"><span class="comment"># 要修改某一个值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[<span class="string">'a'</span>][<span class="string">'A'</span>] = <span class="number">150</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">     A  B  C</span><br><span class="line">a  <span class="number">150</span>  <span class="number">2</span>  <span class="number">3</span></span><br><span class="line">b    <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span></span><br><span class="line"><span class="comment"># 同样可以修改索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.index = [<span class="string">'f'</span>, <span class="string">'g'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">     A  B  C</span><br><span class="line">f  <span class="number">150</span>  <span class="number">2</span>  <span class="number">3</span></span><br><span class="line">g    <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span></span><br><span class="line"><span class="comment"># 增加一行</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[<span class="string">'c'</span>] = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">     A  B  C</span><br><span class="line">f  <span class="number">150</span>  <span class="number">2</span>  <span class="number">3</span></span><br><span class="line">g    <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span></span><br><span class="line">c    <span class="number">1</span>  <span class="number">2</span>  <span class="number">3</span></span><br><span class="line"><span class="comment"># 增加一列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'D'</span>] = [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">     A  B  C   D</span><br><span class="line">f  <span class="number">150</span>  <span class="number">2</span>  <span class="number">3</span>  <span class="number">10</span></span><br><span class="line">g    <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span>  <span class="number">11</span></span><br><span class="line">c    <span class="number">1</span>  <span class="number">2</span>  <span class="number">3</span>  <span class="number">12</span></span><br><span class="line"><span class="comment"># 删除索引为c的记录</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.drop([<span class="string">'c'</span>], axis=<span class="number">0</span>, inplace=<span class="keyword">True</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">     A  B  C   D</span><br><span class="line">f  <span class="number">150</span>  <span class="number">2</span>  <span class="number">3</span>  <span class="number">10</span></span><br><span class="line">g    <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span>  <span class="number">11</span></span><br><span class="line"><span class="comment"># 删除D这一列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> df[<span class="string">'D'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">     A  B  C</span><br><span class="line">f  <span class="number">150</span>  <span class="number">2</span>  <span class="number">3</span></span><br><span class="line">g    <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span></span><br><span class="line"><span class="comment"># 如果要删除多列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.drop([<span class="string">'B'</span>, <span class="string">'C'</span>], axis=<span class="number">1</span>, inplace=<span class="keyword">True</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">     A</span><br><span class="line">f  <span class="number">150</span></span><br><span class="line">g    <span class="number">4</span></span><br><span class="line"><span class="comment"># DataFrame同样支持合并</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>left = pd.DataFrame(&#123;<span class="string">'key'</span>: [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'A'</span>: [<span class="string">'A0'</span>, <span class="string">'A1'</span>, <span class="string">'A2'</span>, <span class="string">'A3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'B'</span>: [<span class="string">'B0'</span>, <span class="string">'B1'</span>, <span class="string">'B2'</span>, <span class="string">'B3'</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>right = pd.DataFrame(&#123;<span class="string">'key'</span>: [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'C'</span>: [<span class="string">'C0'</span>, <span class="string">'C1'</span>, <span class="string">'C2'</span>, <span class="string">'C3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'D'</span>: [<span class="string">'D0'</span>, <span class="string">'D1'</span>, <span class="string">'D2'</span>, <span class="string">'D3'</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>left</span><br><span class="line">  key   A   B</span><br><span class="line"><span class="number">0</span>  K0  A0  B0</span><br><span class="line"><span class="number">1</span>  K1  A1  B1</span><br><span class="line"><span class="number">2</span>  K2  A2  B2</span><br><span class="line"><span class="number">3</span>  K3  A3  B3</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>right</span><br><span class="line">  key   C   D</span><br><span class="line"><span class="number">0</span>  K0  C0  D0</span><br><span class="line"><span class="number">1</span>  K1  C1  D1</span><br><span class="line"><span class="number">2</span>  K2  C2  D2</span><br><span class="line"><span class="number">3</span>  K3  C3  D3</span><br><span class="line"><span class="comment"># 这两个DataFrame都有一个指标叫 key，我们可以按这个key进行合并</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.merge(left=left, right=right, on=<span class="string">"key"</span>)</span><br><span class="line">  key   A   B   C   D</span><br><span class="line"><span class="number">0</span>  K0  A0  B0  C0  D0</span><br><span class="line"><span class="number">1</span>  K1  A1  B1  C1  D1</span><br><span class="line"><span class="number">2</span>  K2  A2  B2  C2  D2</span><br><span class="line"><span class="number">3</span>  K3  A3  B3  C3  D3</span><br><span class="line"><span class="comment"># 假设现在有两个列相同</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>left[<span class="string">'newkey'</span>] = [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K3'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>right[<span class="string">'newkey'</span>] = [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K3'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>left</span><br><span class="line">  key   A   B newkey</span><br><span class="line"><span class="number">0</span>  K0  A0  B0     K0</span><br><span class="line"><span class="number">1</span>  K1  A1  B1     K1</span><br><span class="line"><span class="number">2</span>  K2  A2  B2     K2</span><br><span class="line"><span class="number">3</span>  K3  A3  B3     K3</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>right</span><br><span class="line">  key   C   D newkey</span><br><span class="line"><span class="number">0</span>  K0  C0  D0     K0</span><br><span class="line"><span class="number">1</span>  K1  C1  D1     K1</span><br><span class="line"><span class="number">2</span>  K2  C2  D2     K2</span><br><span class="line"><span class="number">3</span>  K3  C3  D3     K3</span><br><span class="line"><span class="comment"># 直接按两个列进行合并</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.merge(left,right,on=[<span class="string">'key'</span>,<span class="string">'newkey'</span>])</span><br><span class="line">  key   A   B newkey   C   D</span><br><span class="line"><span class="number">0</span>  K0  A0  B0     K0  C0  D0</span><br><span class="line"><span class="number">1</span>  K1  A1  B1     K1  C1  D1</span><br><span class="line"><span class="number">2</span>  K2  A2  B2     K2  C2  D2</span><br><span class="line"><span class="number">3</span>  K3  A3  B3     K3  C3  D3</span><br><span class="line"><span class="comment"># 假设这两列并非完全相同得内容</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>left = pd.DataFrame(&#123;<span class="string">'key1'</span>: [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'key2'</span>: [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'A'</span>: [<span class="string">'A0'</span>, <span class="string">'A1'</span>, <span class="string">'A2'</span>, <span class="string">'A3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'B'</span>: [<span class="string">'B0'</span>, <span class="string">'B1'</span>, <span class="string">'B2'</span>, <span class="string">'B3'</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>right = pd.DataFrame(&#123;<span class="string">'key1'</span>: [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'key2'</span>: [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K4'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'C'</span>: [<span class="string">'C0'</span>, <span class="string">'C1'</span>, <span class="string">'C2'</span>, <span class="string">'C3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'D'</span>: [<span class="string">'D0'</span>, <span class="string">'D1'</span>, <span class="string">'D2'</span>, <span class="string">'D3'</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>left</span><br><span class="line">  key1 key2   A   B</span><br><span class="line"><span class="number">0</span>   K0   K0  A0  B0</span><br><span class="line"><span class="number">1</span>   K1   K1  A1  B1</span><br><span class="line"><span class="number">2</span>   K2   K2  A2  B2</span><br><span class="line"><span class="number">3</span>   K3   K3  A3  B3</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>right</span><br><span class="line">  key1 key2   C   D</span><br><span class="line"><span class="number">0</span>   K0   K0  C0  D0</span><br><span class="line"><span class="number">1</span>   K1   K1  C1  D1</span><br><span class="line"><span class="number">2</span>   K2   K2  C2  D2</span><br><span class="line"><span class="number">3</span>   K3   K4  C3  D3</span><br><span class="line"><span class="comment"># 不一样的记录将被删除</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.merge(left,right,on=[<span class="string">'key1'</span>,<span class="string">'key2'</span>])</span><br><span class="line">  key1 key2   A   B   C   D</span><br><span class="line"><span class="number">0</span>   K0   K0  A0  B0  C0  D0</span><br><span class="line"><span class="number">1</span>   K1   K1  A1  B1  C1  D1</span><br><span class="line"><span class="number">2</span>   K2   K2  A2  B2  C2  D2</span><br><span class="line"><span class="comment"># 如果不想删除</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.merge(left=left, right=right, on=[<span class="string">'key1'</span>,<span class="string">'key2'</span>],how=<span class="string">'outer'</span>)</span><br><span class="line">  key1 key2    A    B    C    D</span><br><span class="line"><span class="number">0</span>   K0   K0   A0   B0   C0   D0</span><br><span class="line"><span class="number">1</span>   K1   K1   A1   B1   C1   D1</span><br><span class="line"><span class="number">2</span>   K2   K2   A2   B2   C2   D2</span><br><span class="line"><span class="number">3</span>   K3   K3   A3   B3  NaN  NaN</span><br><span class="line"><span class="number">4</span>   K3   K4  NaN  NaN   C3   D3</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.merge(left=left, right=right, on=[<span class="string">'key1'</span>,<span class="string">'key2'</span>],how=<span class="string">'outer'</span>,indicator=<span class="keyword">True</span>)</span><br><span class="line">  key1 key2    A    B    C    D      _merge</span><br><span class="line"><span class="number">0</span>   K0   K0   A0   B0   C0   D0        both</span><br><span class="line"><span class="number">1</span>   K1   K1   A1   B1   C1   D1        both</span><br><span class="line"><span class="number">2</span>   K2   K2   A2   B2   C2   D2        both</span><br><span class="line"><span class="number">3</span>   K3   K3   A3   B3  NaN  NaN   left_only</span><br><span class="line"><span class="number">4</span>   K3   K4  NaN  NaN   C3   D3  right_only</span><br><span class="line"><span class="comment"># 这些合并操作什么时候会用到呢？在分析过程中会分模块去过滤一些数据，最后将这些数据合并在一起</span></span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="关于pandas的选项设置"><a href="#关于pandas的选项设置" class="headerlink" title="关于pandas的选项设置"></a>关于pandas的选项设置</h3><p>通过<code>pd.get_option</code>可以查看选项值，具体可以参考 <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.set_option.html">set_option</a></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 我们在打印某个DataFrame的时候，如果数据记录非常多，pandas会隐藏一部分，这个是可以设置最大显示行数的</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.set_option(<span class="string">'display.max_rows'</span>, <span class="number">100</span>)  <span class="comment"># 默认是60</span></span><br><span class="line"><span class="comment"># 同样能够设置显示列的最大值 </span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.set_option(<span class="string">'display.max_columns'</span>, <span class="number">30</span>)</span><br><span class="line"><span class="comment"># 字符串最大显示宽度</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.set_option(<span class="string">'display.max_colwidth'</span>, <span class="number">20</span>)</span><br><span class="line"><span class="comment"># 小数点精度</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.set_option(<span class="string">'display.precision'</span>, <span class="number">2</span>)</span><br></pre></td></tr></table></figure>
<h3 id="pandas对时间的操作"><a href="#pandas对时间的操作" class="headerlink" title="pandas对时间的操作"></a>pandas对时间的操作</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 先看一下原生python代码如何处理时间</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> datetime</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dt = datetime.datetime(year=<span class="number">2018</span>,month=<span class="number">9</span>,day=<span class="number">16</span>,hour=<span class="number">21</span>,minute=<span class="number">37</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(dt)</span><br><span class="line"><span class="number">2018</span><span class="number">-09</span><span class="number">-16</span> <span class="number">21</span>:<span class="number">37</span>:<span class="number">00</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dt.year</span><br><span class="line"><span class="number">2018</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dt.month</span><br><span class="line"><span class="number">9</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dt.day</span><br><span class="line"><span class="number">16</span></span><br><span class="line"><span class="comment"># pandas对时间的处理</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.Timestamp(<span class="string">'2018-09-16'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts = pd.Timestamp(<span class="string">'2018-09-16'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(ts)</span><br><span class="line"><span class="number">2018</span><span class="number">-09</span><span class="number">-16</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts.year</span><br><span class="line"><span class="number">2018</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts.month</span><br><span class="line"><span class="number">9</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts.day</span><br><span class="line"><span class="number">16</span></span><br><span class="line"><span class="comment"># 除了Timestamp函数，还有to_datetime函数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.to_datetime(<span class="string">'2018-09-16'</span>)</span><br><span class="line">Timestamp(<span class="string">'2018-09-16 00:00:00'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.to_datetime(<span class="string">'16/09/2018'</span>)</span><br><span class="line">Timestamp(<span class="string">'2018-09-16 00:00:00'</span>)</span><br><span class="line"><span class="comment"># 获取5天以后的时间</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts + pd.Timedelta(<span class="string">'5 days'</span>)</span><br><span class="line">Timestamp(<span class="string">'2018-09-21 00:00:00'</span>)</span><br></pre></td></tr></table></figure>
<p>可以利用Series结构构建时间记录</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series([<span class="string">'2017-11-24 00:00:00'</span>, <span class="string">'2017-11-25 00:00:00'</span>, <span class="string">'2017-11-26 00:00:00'</span>])</span><br><span class="line"><span class="comment"># 这里的数据类型是字符串，所有dtype为object</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line"><span class="number">0</span>    <span class="number">2017</span><span class="number">-11</span><span class="number">-24</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line"><span class="number">1</span>    <span class="number">2017</span><span class="number">-11</span><span class="number">-25</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line"><span class="number">2</span>    <span class="number">2017</span><span class="number">-11</span><span class="number">-26</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line">dtype: object</span><br><span class="line"><span class="comment"># 转换为datetime类型</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts = pd.to_datetime(s)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts</span><br><span class="line"><span class="number">0</span>   <span class="number">2017</span><span class="number">-11</span><span class="number">-24</span></span><br><span class="line"><span class="number">1</span>   <span class="number">2017</span><span class="number">-11</span><span class="number">-25</span></span><br><span class="line"><span class="number">2</span>   <span class="number">2017</span><span class="number">-11</span><span class="number">-26</span></span><br><span class="line">dtype: datetime64[ns]</span><br><span class="line"><span class="comment"># 对于这种时间属性，可以通过dt获取，如果没有时间属性，则会报错</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts.dt.year</span><br><span class="line"><span class="number">0</span>    <span class="number">2017</span></span><br><span class="line"><span class="number">1</span>    <span class="number">2017</span></span><br><span class="line"><span class="number">2</span>    <span class="number">2017</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts.dt.weekday</span><br><span class="line"><span class="number">0</span>    <span class="number">4</span></span><br><span class="line"><span class="number">1</span>    <span class="number">5</span></span><br><span class="line"><span class="number">2</span>    <span class="number">6</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 我们可以创建时间序列，start：开始时间，periods：时长，间隔时间</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.Series(pd.date_range(start=<span class="string">'2018-09-16'</span>, periods=<span class="number">3</span>, freq=<span class="string">'12H'</span>))</span><br><span class="line"><span class="number">0</span>   <span class="number">2018</span><span class="number">-09</span><span class="number">-16</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line"><span class="number">1</span>   <span class="number">2018</span><span class="number">-09</span><span class="number">-16</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line"><span class="number">2</span>   <span class="number">2018</span><span class="number">-09</span><span class="number">-17</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line">dtype: datetime64[ns]</span><br></pre></td></tr></table></figure>
<p>我们拿真实的数据试验，使用flowdata数据集</p>
<div><div class="fold_hider"><div class="close hider_title">flowdata试验</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">'flowdata.csv'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.head()</span><br><span class="line">                  Time   L06_347  LS06_347  LS06_348</span><br><span class="line"><span class="number">0</span>  <span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.137417</span>  <span class="number">0.097500</span>  <span class="number">0.016833</span></span><br><span class="line"><span class="number">1</span>  <span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">03</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.131250</span>  <span class="number">0.088833</span>  <span class="number">0.016417</span></span><br><span class="line"><span class="number">2</span>  <span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">06</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.113500</span>  <span class="number">0.091250</span>  <span class="number">0.016750</span></span><br><span class="line"><span class="number">3</span>  <span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.135750</span>  <span class="number">0.091500</span>  <span class="number">0.016250</span></span><br><span class="line"><span class="number">4</span>  <span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.140917</span>  <span class="number">0.096167</span>  <span class="number">0.017000</span></span><br><span class="line"><span class="comment"># 转成时间类型</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'Time'</span>] = pd.to_datetime(df[<span class="string">'Time'</span>])</span><br><span class="line"><span class="comment"># 将Time作为index</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.set_index(<span class="string">'Time'</span>)</span><br><span class="line"><span class="comment"># 上面的代码我们先读取出数据，再将字符串的时间转化为时间格式，再将这个列作为index，其实有更方便得处理方式</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">'flowdata.csv'</span>, index_col=<span class="number">0</span>, parse_dates=<span class="keyword">True</span>)</span><br><span class="line"><span class="comment"># 只要数据是以时间为索引，那么我们就可以使用时间进行数据的各种便捷操作了</span></span><br><span class="line"><span class="comment"># 根据时间索引切片筛选出记录</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[pd.Timestamp(<span class="string">'2012-01-01 09:00'</span>):pd.Timestamp(<span class="string">'2012-01-01 19:00'</span>)]</span><br><span class="line">                      L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.330750</span>  <span class="number">0.293583</span>  <span class="number">0.029750</span></span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.295000</span>  <span class="number">0.285167</span>  <span class="number">0.031750</span></span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">15</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.301417</span>  <span class="number">0.287750</span>  <span class="number">0.031417</span></span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">18</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.322083</span>  <span class="number">0.304167</span>  <span class="number">0.038083</span></span><br><span class="line"><span class="comment"># df[('2012-01-01 09:00'):('2012-01-01 19:00')] 这种方式等价</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[(<span class="string">'2012-01-01 09:00'</span>):(<span class="string">'2012-01-01 19:00'</span>)]</span><br><span class="line">                      L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.330750</span>  <span class="number">0.293583</span>  <span class="number">0.029750</span></span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.295000</span>  <span class="number">0.285167</span>  <span class="number">0.031750</span></span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">15</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.301417</span>  <span class="number">0.287750</span>  <span class="number">0.031417</span></span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">18</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.322083</span>  <span class="number">0.304167</span>  <span class="number">0.038083</span></span><br><span class="line"><span class="comment"># 我们可以直接获取某一年的数据</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[<span class="string">'2013'</span>]</span><br><span class="line">                      L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">1.688333</span>  <span class="number">1.688333</span>  <span class="number">0.207333</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">03</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">2.693333</span>  <span class="number">2.693333</span>  <span class="number">0.201500</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">06</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">2.220833</span>  <span class="number">2.220833</span>  <span class="number">0.166917</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">2.055000</span>  <span class="number">2.055000</span>  <span class="number">0.175667</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">1.710000</span>  <span class="number">1.710000</span>  <span class="number">0.129583</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">15</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">1.420000</span>  <span class="number">1.420000</span>  <span class="number">0.096333</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">18</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">1.178583</span>  <span class="number">1.178583</span>  <span class="number">0.083083</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">21</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.898250</span>  <span class="number">0.898250</span>  <span class="number">0.077167</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.860000</span>  <span class="number">0.860000</span>  <span class="number">0.075000</span></span><br><span class="line"><span class="comment"># 也可以指定区间</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[<span class="string">'2012-01'</span>:<span class="string">'2012-03'</span>]</span><br><span class="line">                      L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.307167</span>  <span class="number">0.273917</span>  <span class="number">0.028000</span></span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">03</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.302917</span>  <span class="number">0.270833</span>  <span class="number">0.030583</span></span><br><span class="line"><span class="meta">... </span>... ... ...</span><br><span class="line"><span class="comment"># 由于index为时间格式，因此可以直接使用month等属性</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[df.index.month==<span class="number">1</span>]</span><br><span class="line">                      L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.137417</span>  <span class="number">0.097500</span>  <span class="number">0.016833</span></span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">03</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.131250</span>  <span class="number">0.088833</span>  <span class="number">0.016417</span></span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">06</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.113500</span>  <span class="number">0.091250</span>  <span class="number">0.016750</span></span><br><span class="line"><span class="meta">... </span>... ... ...</span><br><span class="line"><span class="comment"># 复杂一点的过滤</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[(df.index.hour &gt; <span class="number">8</span>) &amp; (df.index.hour &lt; <span class="number">12</span>)]</span><br><span class="line">                      L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.135750</span>  <span class="number">0.091500</span>  <span class="number">0.016250</span></span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-02</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.141917</span>  <span class="number">0.097083</span>  <span class="number">0.016417</span></span><br><span class="line"><span class="meta">... </span>... ... ...</span><br><span class="line"><span class="comment"># 与上面等价</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.between_time(<span class="string">'08:00'</span>, <span class="string">'12:00'</span>)</span><br><span class="line">                      L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.135750</span>  <span class="number">0.091500</span>  <span class="number">0.016250</span></span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-02</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.141917</span>  <span class="number">0.097083</span>  <span class="number">0.016417</span></span><br><span class="line"><span class="meta">... </span>... ... ...</span><br><span class="line"><span class="comment"># 我们也可以按时间进行重采样</span></span><br><span class="line"><span class="comment"># 比如按天算均值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.resample(<span class="string">'D'</span>).mean()</span><br><span class="line">             L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.125010</span>  <span class="number">0.092281</span>  <span class="number">0.016635</span></span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">0.124146</span>  <span class="number">0.095781</span>  <span class="number">0.016406</span></span><br><span class="line"><span class="meta">... </span>... ... ...</span><br><span class="line"><span class="comment"># 三天重采样一次</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.resample(<span class="string">'3D'</span>).mean()</span><br><span class="line">             L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.120906</span>  <span class="number">0.091201</span>  <span class="number">0.016378</span></span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.121594</span>  <span class="number">0.091708</span>  <span class="number">0.016670</span></span><br><span class="line"><span class="meta">... </span>... ... ...</span><br><span class="line"><span class="comment"># 按月重采样一次</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.resample(<span class="string">'M'</span>).mean()</span><br><span class="line">             L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-31</span>  <span class="number">0.517864</span>  <span class="number">0.536660</span>  <span class="number">0.045597</span></span><br><span class="line"><span class="number">2009</span><span class="number">-02</span><span class="number">-28</span>  <span class="number">0.516847</span>  <span class="number">0.529987</span>  <span class="number">0.047238</span></span><br><span class="line"><span class="meta">... </span>... ... ...</span><br><span class="line"><span class="comment"># 也可以直接画图，如果使用jupyter notebook，可以使用如下指令</span></span><br><span class="line">%matplotlib notebook</span><br><span class="line">df.resample(<span class="string">'M'</span>).mean().plot()</span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="字符串处理"><a href="#字符串处理" class="headerlink" title="字符串处理"></a>字符串处理</h3><p>这些处理方法都只能作用于Series于Index结构</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series([<span class="string">'A'</span>, <span class="string">'b'</span>, <span class="string">'B'</span>, <span class="string">'gaer'</span>, <span class="string">'AGER'</span>, np.nan])</span><br><span class="line"><span class="comment"># 字符串的大小写转换</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.lower()</span><br><span class="line"><span class="number">0</span>       a</span><br><span class="line"><span class="number">1</span>       b</span><br><span class="line"><span class="number">2</span>       b</span><br><span class="line"><span class="number">3</span>    gaer</span><br><span class="line"><span class="number">4</span>    ager</span><br><span class="line"><span class="number">5</span>     NaN</span><br><span class="line">dtype: object</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.upper()</span><br><span class="line"><span class="number">0</span>       A</span><br><span class="line"><span class="number">1</span>       B</span><br><span class="line"><span class="number">2</span>       B</span><br><span class="line"><span class="number">3</span>    GAER</span><br><span class="line"><span class="number">4</span>    AGER</span><br><span class="line"><span class="number">5</span>     NaN</span><br><span class="line">dtype: object</span><br><span class="line"><span class="comment"># 字符串的长度</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.len()</span><br><span class="line"><span class="number">0</span>    <span class="number">1.0</span></span><br><span class="line"><span class="number">1</span>    <span class="number">1.0</span></span><br><span class="line"><span class="number">2</span>    <span class="number">1.0</span></span><br><span class="line"><span class="number">3</span>    <span class="number">4.0</span></span><br><span class="line"><span class="number">4</span>    <span class="number">4.0</span></span><br><span class="line"><span class="number">5</span>    NaN</span><br><span class="line">dtype: float64</span><br><span class="line"><span class="comment"># 去空格</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index = pd.Index([<span class="string">'   jack'</span>, <span class="string">'   straw   '</span>, <span class="string">'xian'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index.str.strip()</span><br><span class="line">Index([<span class="string">'jack'</span>, <span class="string">'straw'</span>, <span class="string">'xian'</span>], dtype=<span class="string">'object'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index.str.lstrip()</span><br><span class="line">Index([<span class="string">'jack'</span>, <span class="string">'straw   '</span>, <span class="string">'xian'</span>], dtype=<span class="string">'object'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index.str.rstrip()</span><br><span class="line">Index([<span class="string">'   jack'</span>, <span class="string">'   straw'</span>, <span class="string">'xian'</span>], dtype=<span class="string">'object'</span>)</span><br><span class="line"><span class="comment"># 字符串替换</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(np.random.randn(<span class="number">3</span>,<span class="number">2</span>), columns=[<span class="string">'A a'</span>, <span class="string">'B b'</span>], index=range(<span class="number">3</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">        A a       B b</span><br><span class="line"><span class="number">0</span> <span class="number">-0.863814</span> <span class="number">-0.595908</span></span><br><span class="line"><span class="number">1</span>  <span class="number">0.512255</span>  <span class="number">0.447264</span></span><br><span class="line"><span class="number">2</span>  <span class="number">1.130682</span>  <span class="number">1.472386</span></span><br><span class="line"><span class="comment"># 修改列名</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.columns = df.columns.str.replace(<span class="string">' '</span>, <span class="string">'_'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">        A_a       B_b</span><br><span class="line"><span class="number">0</span> <span class="number">-0.863814</span> <span class="number">-0.595908</span></span><br><span class="line"><span class="number">1</span>  <span class="number">0.512255</span>  <span class="number">0.447264</span></span><br><span class="line"><span class="number">2</span>  <span class="number">1.130682</span>  <span class="number">1.472386</span></span><br><span class="line"><span class="comment"># 字符串切分</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series([<span class="string">'a_b_C'</span>, <span class="string">'c_d_e'</span>, <span class="string">'f_g_h'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.split(<span class="string">'_'</span>)</span><br><span class="line"><span class="number">0</span>    [a, b, C]</span><br><span class="line"><span class="number">1</span>    [c, d, e]</span><br><span class="line"><span class="number">2</span>    [f, g, h]</span><br><span class="line">dtype: object</span><br><span class="line"><span class="comment"># 如果允许expand，则会变成一个DataFrame结构</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.split(<span class="string">'_'</span>, expand = <span class="keyword">True</span>)</span><br><span class="line">   <span class="number">0</span>  <span class="number">1</span>  <span class="number">2</span></span><br><span class="line"><span class="number">0</span>  a  b  C</span><br><span class="line"><span class="number">1</span>  c  d  e</span><br><span class="line"><span class="number">2</span>  f  g  h</span><br><span class="line"><span class="comment"># 也可以限制切分次数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.split(<span class="string">'_'</span>, expand=<span class="keyword">True</span>, n=<span class="number">1</span>)</span><br><span class="line">   <span class="number">0</span>    <span class="number">1</span></span><br><span class="line"><span class="number">0</span>  a  b_C</span><br><span class="line"><span class="number">1</span>  c  d_e</span><br><span class="line"><span class="number">2</span>  f  g_h</span><br><span class="line"><span class="comment"># 可以查看值是否包含某个字符串序列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series([<span class="string">'abcde'</span>, <span class="string">'gggbcdiii'</span>, <span class="string">'abiuyf'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.contains(<span class="string">'bcd'</span>)</span><br><span class="line"><span class="number">0</span>     <span class="keyword">True</span></span><br><span class="line"><span class="number">1</span>     <span class="keyword">True</span></span><br><span class="line"><span class="number">2</span>    <span class="keyword">False</span></span><br><span class="line">dtype: bool</span><br><span class="line"><span class="comment"># 最终是一个DataFrame</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series([<span class="string">'a'</span>, <span class="string">'a|b'</span>, <span class="string">'a|c'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.get_dummies(sep=<span class="string">'|'</span>)</span><br><span class="line">   a  b  c</span><br><span class="line"><span class="number">0</span>  <span class="number">1</span>  <span class="number">0</span>  <span class="number">0</span></span><br><span class="line"><span class="number">1</span>  <span class="number">1</span>  <span class="number">1</span>  <span class="number">0</span></span><br><span class="line"><span class="number">2</span>  <span class="number">1</span>  <span class="number">0</span>  <span class="number">1</span></span><br></pre></td></tr></table></figure>
<h2 id="高级主题"><a href="#高级主题" class="headerlink" title="高级主题"></a>高级主题</h2><h3 id="数据透视表"><a href="#数据透视表" class="headerlink" title="数据透视表"></a>数据透视表</h3><p>你可知道数据透视表的作用是什么？搞清楚下面的例子，你便能够了解</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 测试数据如下</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>example = pd.DataFrame(&#123;<span class="string">'Month'</span>: [<span class="string">"January"</span>, <span class="string">"January"</span>, <span class="string">"January"</span>, <span class="string">"January"</span>, <span class="string">"February"</span>, <span class="string">"February"</span>, <span class="string">"February"</span>, <span class="string">"February"</span>, <span class="string">"March"</span>, <span class="string">"March"</span>, <span class="string">"March"</span>, <span class="string">"March"</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'Category'</span>: [<span class="string">"Transportation"</span>, <span class="string">"Grocery"</span>, <span class="string">"Household"</span>, <span class="string">"Entertainment"</span>, <span class="string">"Transportation"</span>, <span class="string">"Grocery"</span>, <span class="string">"Household"</span>, <span class="string">"Entertainment"</span>, <span class="string">"Transportation"</span>, <span class="string">"Grocery"</span>, <span class="string">"Household"</span>, <span class="string">"Entertainme</span></span><br><span class="line"><span class="string">nt"</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'Amount'</span>: [<span class="number">74.</span>, <span class="number">235.</span>, <span class="number">175.</span>, <span class="number">100.</span>, <span class="number">115.</span>, <span class="number">240.</span>, <span class="number">225.</span>, <span class="number">125.</span>, <span class="number">90.</span>, <span class="number">260.</span>, <span class="number">200.</span>, <span class="number">120.</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>example</span><br><span class="line">       Month        Category  Amount</span><br><span class="line"><span class="number">0</span>    January  Transportation    <span class="number">74.0</span></span><br><span class="line"><span class="number">1</span>    January         Grocery   <span class="number">235.0</span></span><br><span class="line"><span class="number">2</span>    January       Household   <span class="number">175.0</span></span><br><span class="line"><span class="number">3</span>    January   Entertainment   <span class="number">100.0</span></span><br><span class="line"><span class="number">4</span>   February  Transportation   <span class="number">115.0</span></span><br><span class="line"><span class="number">5</span>   February         Grocery   <span class="number">240.0</span></span><br><span class="line"><span class="number">6</span>   February       Household   <span class="number">225.0</span></span><br><span class="line"><span class="number">7</span>   February   Entertainment   <span class="number">125.0</span></span><br><span class="line"><span class="number">8</span>      March  Transportation    <span class="number">90.0</span></span><br><span class="line"><span class="number">9</span>      March         Grocery   <span class="number">260.0</span></span><br><span class="line"><span class="number">10</span>     March       Household   <span class="number">200.0</span></span><br><span class="line"><span class="number">11</span>     March   Entertainment   <span class="number">120.0</span></span><br><span class="line"><span class="comment"># 将散乱的无结构数据转成有结构的数据，换了一种视角，是否更加清楚呢</span></span><br><span class="line"><span class="comment"># 仔细理解一下，透视表得作用</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>example_pivot = example.pivot(index=<span class="string">'Category'</span>, columns=<span class="string">'Month'</span>, values=<span class="string">'Amount'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>example_pivot</span><br><span class="line">Month           February  January  March</span><br><span class="line">Category</span><br><span class="line">Entertainment      <span class="number">125.0</span>    <span class="number">100.0</span>  <span class="number">120.0</span></span><br><span class="line">Grocery            <span class="number">240.0</span>    <span class="number">235.0</span>  <span class="number">260.0</span></span><br><span class="line">Household          <span class="number">225.0</span>    <span class="number">175.0</span>  <span class="number">200.0</span></span><br><span class="line">Transportation     <span class="number">115.0</span>     <span class="number">74.0</span>   <span class="number">90.0</span></span><br><span class="line"><span class="comment"># 使用透视表后，再进行一些统计就方便多了</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>example_pivot.sum(axis=<span class="number">0</span>)</span><br><span class="line">Month</span><br><span class="line">February    <span class="number">705.0</span></span><br><span class="line">January     <span class="number">584.0</span></span><br><span class="line">March       <span class="number">670.0</span></span><br><span class="line">dtype: float64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>example_pivot.sum(axis=<span class="number">1</span>)</span><br><span class="line">Category</span><br><span class="line">Entertainment     <span class="number">345.0</span></span><br><span class="line">Grocery           <span class="number">735.0</span></span><br><span class="line">Household         <span class="number">600.0</span></span><br><span class="line">Transportation    <span class="number">279.0</span></span><br><span class="line">dtype: float64</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">泰坦尼克号数据实战</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">"titanic_train.csv"</span>)</span><br><span class="line"><span class="comment"># 统计不同性别在不同船舱登记的平均价格如何，这里默认进行的操作就是求均值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.pivot_table(index=<span class="string">'Sex'</span>, columns=<span class="string">'Pclass'</span>, values=<span class="string">'Fare'</span>)</span><br><span class="line">Pclass           <span class="number">1</span>          <span class="number">2</span>          <span class="number">3</span></span><br><span class="line">Sex</span><br><span class="line">female  <span class="number">106.125798</span>  <span class="number">21.970121</span>  <span class="number">16.118810</span></span><br><span class="line">male     <span class="number">67.226127</span>  <span class="number">19.741782</span>  <span class="number">12.661633</span></span><br><span class="line"><span class="comment"># 这里我们统计最大值，这里表示不同性别在不同船舱等级的一个最大的花费</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.pivot_table(index=<span class="string">'Sex'</span>, columns=<span class="string">'Pclass'</span>, values=<span class="string">'Fare'</span>, aggfunc=<span class="string">'max'</span>)</span><br><span class="line">Pclass         <span class="number">1</span>     <span class="number">2</span>      <span class="number">3</span></span><br><span class="line">Sex</span><br><span class="line">female  <span class="number">512.3292</span>  <span class="number">65.0</span>  <span class="number">69.55</span></span><br><span class="line">male    <span class="number">512.3292</span>  <span class="number">73.5</span>  <span class="number">69.55</span></span><br><span class="line"><span class="comment"># 也可以计数，这里表示不同性别在不同等级船舱的人数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.pivot_table(index=<span class="string">'Sex'</span>, columns=<span class="string">'Pclass'</span>, values=<span class="string">'Fare'</span>, aggfunc=<span class="string">'count'</span>)</span><br><span class="line">Pclass    <span class="number">1</span>    <span class="number">2</span>    <span class="number">3</span></span><br><span class="line">Sex</span><br><span class="line">female   <span class="number">94</span>   <span class="number">76</span>  <span class="number">144</span></span><br><span class="line">male    <span class="number">122</span>  <span class="number">108</span>  <span class="number">347</span></span><br><span class="line"><span class="comment"># 提到计数，这里有个便捷的函数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.crosstab(index=df[<span class="string">'Sex'</span>], columns=df[<span class="string">'Pclass'</span>])</span><br><span class="line">Pclass    <span class="number">1</span>    <span class="number">2</span>    <span class="number">3</span></span><br><span class="line">Sex</span><br><span class="line">female   <span class="number">94</span>   <span class="number">76</span>  <span class="number">144</span></span><br><span class="line">male    <span class="number">122</span>  <span class="number">108</span>  <span class="number">347</span></span><br><span class="line"><span class="comment"># 统计其他指标，比如统计不同船舱的男女获救情况</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.pivot_table(index=<span class="string">'Pclass'</span>, columns=<span class="string">'Sex'</span>, values=<span class="string">'Survived'</span>, aggfunc=<span class="string">'mean'</span>)</span><br><span class="line">Sex       female      male</span><br><span class="line">Pclass</span><br><span class="line"><span class="number">1</span>       <span class="number">0.968085</span>  <span class="number">0.368852</span></span><br><span class="line"><span class="number">2</span>       <span class="number">0.921053</span>  <span class="number">0.157407</span></span><br><span class="line"><span class="number">3</span>       <span class="number">0.500000</span>  <span class="number">0.135447</span></span><br><span class="line"><span class="comment"># 再添加条件，统计未成年人中男性与女性的获救情况</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'Underaged'</span>] = df[<span class="string">'Age'</span>] &lt; <span class="number">18</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.pivot_table(index=<span class="string">'Underaged'</span>, columns=<span class="string">'Sex'</span>, values=<span class="string">'Survived'</span>, aggfunc=<span class="string">'mean'</span>)</span><br><span class="line">Sex          female      male</span><br><span class="line">Underaged</span><br><span class="line"><span class="keyword">False</span>      <span class="number">0.752896</span>  <span class="number">0.165703</span></span><br><span class="line"><span class="keyword">True</span>       <span class="number">0.690909</span>  <span class="number">0.396552</span></span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="排序操作"><a href="#排序操作" class="headerlink" title="排序操作"></a>排序操作</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = pd.DataFrame(&#123;<span class="string">'group'</span>:[<span class="string">'a'</span>,<span class="string">'a'</span>,<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'b'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'c'</span>,<span class="string">'c'</span>], <span class="string">'data'</span>:[<span class="number">4</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">12</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">7</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data</span><br><span class="line">  group  data</span><br><span class="line"><span class="number">0</span>     a     <span class="number">4</span></span><br><span class="line"><span class="number">1</span>     a     <span class="number">3</span></span><br><span class="line"><span class="number">2</span>     a     <span class="number">2</span></span><br><span class="line"><span class="number">3</span>     b     <span class="number">1</span></span><br><span class="line"><span class="number">4</span>     b    <span class="number">12</span></span><br><span class="line"><span class="number">5</span>     b     <span class="number">3</span></span><br><span class="line"><span class="number">6</span>     c     <span class="number">4</span></span><br><span class="line"><span class="number">7</span>     c     <span class="number">5</span></span><br><span class="line"><span class="number">8</span>     c     <span class="number">7</span></span><br><span class="line"><span class="comment"># 在保证group列的值降序的情况下，data的值升序</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data.sort_values(by=[<span class="string">'group'</span>, <span class="string">'data'</span>], ascending=[<span class="keyword">False</span>, <span class="keyword">True</span>], inplace=<span class="keyword">True</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data</span><br><span class="line">  group  data</span><br><span class="line"><span class="number">6</span>     c     <span class="number">4</span></span><br><span class="line"><span class="number">7</span>     c     <span class="number">5</span></span><br><span class="line"><span class="number">8</span>     c     <span class="number">7</span></span><br><span class="line"><span class="number">3</span>     b     <span class="number">1</span></span><br><span class="line"><span class="number">5</span>     b     <span class="number">3</span></span><br><span class="line"><span class="number">4</span>     b    <span class="number">12</span></span><br><span class="line"><span class="number">2</span>     a     <span class="number">2</span></span><br><span class="line"><span class="number">1</span>     a     <span class="number">3</span></span><br><span class="line"><span class="number">0</span>     a     <span class="number">4</span></span><br></pre></td></tr></table></figure>
<h3 id="去重"><a href="#去重" class="headerlink" title="去重"></a>去重</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = pd.DataFrame(&#123;<span class="string">'k1'</span>:[<span class="string">'one'</span>]*<span class="number">3</span>+[<span class="string">'two'</span>]*<span class="number">4</span>,<span class="string">'k2'</span>:[<span class="number">3</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">4</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data</span><br><span class="line">    k1  k2</span><br><span class="line"><span class="number">0</span>  one   <span class="number">3</span></span><br><span class="line"><span class="number">1</span>  one   <span class="number">2</span></span><br><span class="line"><span class="number">2</span>  one   <span class="number">1</span></span><br><span class="line"><span class="number">3</span>  two   <span class="number">3</span></span><br><span class="line"><span class="number">4</span>  two   <span class="number">3</span></span><br><span class="line"><span class="number">5</span>  two   <span class="number">4</span></span><br><span class="line"><span class="number">6</span>  two   <span class="number">4</span></span><br><span class="line"><span class="comment"># 上面的数据有重复值，我们需要删除，只有整条记录相同才会删除</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data.drop_duplicates()</span><br><span class="line">    k1  k2</span><br><span class="line"><span class="number">0</span>  one   <span class="number">3</span></span><br><span class="line"><span class="number">1</span>  one   <span class="number">2</span></span><br><span class="line"><span class="number">2</span>  one   <span class="number">1</span></span><br><span class="line"><span class="number">3</span>  two   <span class="number">3</span></span><br><span class="line"><span class="number">5</span>  two   <span class="number">4</span></span><br><span class="line"><span class="comment"># 保证某列的数据时唯一不重复，需要指定去重的列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data.drop_duplicates(subset=<span class="string">'k1'</span>)</span><br><span class="line">    k1  k2</span><br><span class="line"><span class="number">0</span>  one   <span class="number">3</span></span><br><span class="line"><span class="number">3</span>  two   <span class="number">3</span></span><br></pre></td></tr></table></figure>
<h3 id="自定义处理方法"><a href="#自定义处理方法" class="headerlink" title="自定义处理方法"></a>自定义处理方法</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = pd.DataFrame(&#123;<span class="string">'food'</span>:[<span class="string">'A1'</span>,<span class="string">'A2'</span>,<span class="string">'B1'</span>,<span class="string">'B2'</span>,<span class="string">'B3'</span>,<span class="string">'C1'</span>,<span class="string">'C2'</span>], <span class="string">'data'</span>:[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data</span><br><span class="line">  food  data</span><br><span class="line"><span class="number">0</span>   A1     <span class="number">1</span></span><br><span class="line"><span class="number">1</span>   A2     <span class="number">2</span></span><br><span class="line"><span class="number">2</span>   B1     <span class="number">3</span></span><br><span class="line"><span class="number">3</span>   B2     <span class="number">4</span></span><br><span class="line"><span class="number">4</span>   B3     <span class="number">5</span></span><br><span class="line"><span class="number">5</span>   C1     <span class="number">6</span></span><br><span class="line"><span class="number">6</span>   C2     <span class="number">7</span></span><br><span class="line"><span class="comment"># 我想要将A1、A2都归为A，B1、B2都归为B，C1、C2都归为C</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">food_map</span><span class="params">(series)</span>:</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">if</span> series[<span class="string">'food'</span>] == <span class="string">'A1'</span>:</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">return</span> <span class="string">'A'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">elif</span> series[<span class="string">'food'</span>] == <span class="string">'A2'</span>:</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">return</span> <span class="string">'A'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">elif</span> series[<span class="string">'food'</span>] == <span class="string">'B1'</span>:</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">return</span> <span class="string">'B'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">elif</span> series[<span class="string">'food'</span>] == <span class="string">'B2'</span>:</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">return</span> <span class="string">'B'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">elif</span> series[<span class="string">'food'</span>] == <span class="string">'B3'</span>:</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">return</span> <span class="string">'B'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">elif</span> series[<span class="string">'food'</span>] == <span class="string">'C1'</span>:</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">return</span> <span class="string">'C'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">elif</span> series[<span class="string">'food'</span>] == <span class="string">'C2'</span>:</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">return</span> <span class="string">'C'</span></span><br><span class="line"><span class="comment"># 使用apply函数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data[<span class="string">'food_map'</span>] = data.apply(food_map, axis=<span class="string">'columns'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data</span><br><span class="line">  food  data food_map</span><br><span class="line"><span class="number">0</span>   A1     <span class="number">1</span>        A</span><br><span class="line"><span class="number">1</span>   A2     <span class="number">2</span>        A</span><br><span class="line"><span class="number">2</span>   B1     <span class="number">3</span>        B</span><br><span class="line"><span class="number">3</span>   B2     <span class="number">4</span>        B</span><br><span class="line"><span class="number">4</span>   B3     <span class="number">5</span>        B</span><br><span class="line"><span class="number">5</span>   C1     <span class="number">6</span>        C</span><br><span class="line"><span class="number">6</span>   C2     <span class="number">7</span>        C</span><br><span class="line"><span class="comment"># 使用map函数也能完成目标</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>food2Upper = &#123;</span><br><span class="line"><span class="meta">... </span>    <span class="string">'A1'</span>:<span class="string">'A'</span>,</span><br><span class="line"><span class="meta">... </span>    <span class="string">'A2'</span>:<span class="string">'A'</span>,</span><br><span class="line"><span class="meta">... </span>    <span class="string">'B1'</span>:<span class="string">'B'</span>,</span><br><span class="line"><span class="meta">... </span>    <span class="string">'B2'</span>:<span class="string">'B'</span>,</span><br><span class="line"><span class="meta">... </span>    <span class="string">'B3'</span>:<span class="string">'B'</span>,</span><br><span class="line"><span class="meta">... </span>    <span class="string">'C1'</span>:<span class="string">'C'</span>,</span><br><span class="line"><span class="meta">... </span>    <span class="string">'C2'</span>:<span class="string">'C'</span></span><br><span class="line"><span class="meta">... </span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data[<span class="string">'upper'</span>] = data[<span class="string">'food'</span>].map(food2Upper)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data</span><br><span class="line">  food  data food_map upper</span><br><span class="line"><span class="number">0</span>   A1     <span class="number">1</span>        A     A</span><br><span class="line"><span class="number">1</span>   A2     <span class="number">2</span>        A     A</span><br><span class="line"><span class="number">2</span>   B1     <span class="number">3</span>        B     B</span><br><span class="line"><span class="number">3</span>   B2     <span class="number">4</span>        B     B</span><br><span class="line"><span class="number">4</span>   B3     <span class="number">5</span>        B     B</span><br><span class="line"><span class="number">5</span>   C1     <span class="number">6</span>        C     C</span><br><span class="line"><span class="number">6</span>   C2     <span class="number">7</span>        C     C</span><br><span class="line"><span class="comment"># 请仔细去理解</span></span><br></pre></td></tr></table></figure>
<h3 id="使用assign新增一列，可以使用其他列进行计算"><a href="#使用assign新增一列，可以使用其他列进行计算" class="headerlink" title="使用assign新增一列，可以使用其他列进行计算"></a>使用assign新增一列，可以使用其他列进行计算</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(&#123;<span class="string">'data1'</span>: np.random.randn(<span class="number">5</span>), <span class="string">'data2'</span>: np.random.randn(<span class="number">5</span>)&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">      data1     data2</span><br><span class="line"><span class="number">0</span>  <span class="number">2.453417</span> <span class="number">-0.108647</span></span><br><span class="line"><span class="number">1</span>  <span class="number">1.131228</span>  <span class="number">0.056595</span></span><br><span class="line"><span class="number">2</span> <span class="number">-0.406572</span> <span class="number">-0.675934</span></span><br><span class="line"><span class="number">3</span> <span class="number">-0.534769</span>  <span class="number">0.608112</span></span><br><span class="line"><span class="number">4</span> <span class="number">-0.065837</span> <span class="number">-1.373105</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.assign(ration=df[<span class="string">'data1'</span>]/df[<span class="string">'data2'</span>])</span><br><span class="line">      data1     data2     ration</span><br><span class="line"><span class="number">0</span>  <span class="number">2.453417</span> <span class="number">-0.108647</span> <span class="number">-22.581632</span></span><br><span class="line"><span class="number">1</span>  <span class="number">1.131228</span>  <span class="number">0.056595</span>  <span class="number">19.988231</span></span><br><span class="line"><span class="number">2</span> <span class="number">-0.406572</span> <span class="number">-0.675934</span>   <span class="number">0.601497</span></span><br><span class="line"><span class="number">3</span> <span class="number">-0.534769</span>  <span class="number">0.608112</span>  <span class="number">-0.879392</span></span><br><span class="line"><span class="number">4</span> <span class="number">-0.065837</span> <span class="number">-1.373105</span>   <span class="number">0.047947</span></span><br></pre></td></tr></table></figure>
<h3 id="其他处理"><a href="#其他处理" class="headerlink" title="其他处理"></a>其他处理</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 连续数据的离散化，将多个年龄放到不同区间</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ages = [<span class="number">15</span>,<span class="number">18</span>,<span class="number">20</span>,<span class="number">21</span>,<span class="number">22</span>,<span class="number">34</span>,<span class="number">41</span>,<span class="number">52</span>,<span class="number">63</span>,<span class="number">79</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>bins = [<span class="number">10</span>,<span class="number">40</span>,<span class="number">80</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>bins_res = pd.cut(ages, bins)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>bins_res.codes</span><br><span class="line">array([<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>], dtype=int8)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.value_counts(bins_res)</span><br><span class="line">(<span class="number">10</span>, <span class="number">40</span>]    <span class="number">6</span></span><br><span class="line">(<span class="number">40</span>, <span class="number">80</span>]    <span class="number">4</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 上面都没有指定名字，我们可以指定组名</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ages = [<span class="number">15</span>,<span class="number">18</span>,<span class="number">20</span>,<span class="number">21</span>,<span class="number">22</span>,<span class="number">34</span>,<span class="number">41</span>,<span class="number">52</span>,<span class="number">63</span>,<span class="number">79</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>group_names = [<span class="string">'Yonth'</span>, <span class="string">'Mille'</span>, <span class="string">'Old'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.value_counts(pd.cut(ages, [<span class="number">10</span>,<span class="number">20</span>,<span class="number">50</span>,<span class="number">80</span>], labels=group_names))</span><br><span class="line">Mille    <span class="number">4</span></span><br><span class="line">Old      <span class="number">3</span></span><br><span class="line">Yonth    <span class="number">3</span></span><br><span class="line">dtype: int64</span><br><span class="line"></span><br><span class="line"><span class="comment"># 处理缺失值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame([range(<span class="number">3</span>),[<span class="number">0</span>, np.nan,<span class="number">0</span>],[<span class="number">0</span>,<span class="number">0</span>,np.nan],range(<span class="number">3</span>)])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">   <span class="number">0</span>    <span class="number">1</span>    <span class="number">2</span></span><br><span class="line"><span class="number">0</span>  <span class="number">0</span>  <span class="number">1.0</span>  <span class="number">2.0</span></span><br><span class="line"><span class="number">1</span>  <span class="number">0</span>  NaN  <span class="number">0.0</span></span><br><span class="line"><span class="number">2</span>  <span class="number">0</span>  <span class="number">0.0</span>  NaN</span><br><span class="line"><span class="number">3</span>  <span class="number">0</span>  <span class="number">1.0</span>  <span class="number">2.0</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.isnull()</span><br><span class="line">       <span class="number">0</span>      <span class="number">1</span>      <span class="number">2</span></span><br><span class="line"><span class="number">0</span>  <span class="keyword">False</span>  <span class="keyword">False</span>  <span class="keyword">False</span></span><br><span class="line"><span class="number">1</span>  <span class="keyword">False</span>   <span class="keyword">True</span>  <span class="keyword">False</span></span><br><span class="line"><span class="number">2</span>  <span class="keyword">False</span>  <span class="keyword">False</span>   <span class="keyword">True</span></span><br><span class="line"><span class="number">3</span>  <span class="keyword">False</span>  <span class="keyword">False</span>  <span class="keyword">False</span></span><br><span class="line"><span class="comment"># 检查每个记录是否有缺失值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.isnull().any(axis=<span class="number">1</span>)</span><br><span class="line"><span class="number">0</span>    <span class="keyword">False</span></span><br><span class="line"><span class="number">1</span>     <span class="keyword">True</span></span><br><span class="line"><span class="number">2</span>     <span class="keyword">True</span></span><br><span class="line"><span class="number">3</span>    <span class="keyword">False</span></span><br><span class="line">dtype: bool</span><br><span class="line"><span class="comment"># 同样，看每个列是否有缺失值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.isnull().any(axis=<span class="number">0</span>)     <span class="comment"># 默认的axis就是0</span></span><br><span class="line"><span class="number">0</span>    <span class="keyword">False</span></span><br><span class="line"><span class="number">1</span>     <span class="keyword">True</span></span><br><span class="line"><span class="number">2</span>     <span class="keyword">True</span></span><br><span class="line">dtype: bool</span><br><span class="line"><span class="comment"># 对于有缺失值的地方，使用用一个数据去填充</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.fillna(<span class="number">5</span>)</span><br><span class="line">   <span class="number">0</span>    <span class="number">1</span>    <span class="number">2</span></span><br><span class="line"><span class="number">0</span>  <span class="number">0</span>  <span class="number">1.0</span>  <span class="number">2.0</span></span><br><span class="line"><span class="number">1</span>  <span class="number">0</span>  <span class="number">5.0</span>  <span class="number">0.0</span></span><br><span class="line"><span class="number">2</span>  <span class="number">0</span>  <span class="number">0.0</span>  <span class="number">5.0</span></span><br><span class="line"><span class="number">3</span>  <span class="number">0</span>  <span class="number">1.0</span>  <span class="number">2.0</span></span><br><span class="line"><span class="comment"># 过滤出有缺失值的行</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[df.isnull().any(axis=<span class="number">1</span>)]</span><br><span class="line">   <span class="number">0</span>    <span class="number">1</span>    <span class="number">2</span></span><br><span class="line"><span class="number">1</span>  <span class="number">0</span>  NaN  <span class="number">0.0</span></span><br><span class="line"><span class="number">2</span>  <span class="number">0</span>  <span class="number">0.0</span>  NaN</span><br><span class="line"><span class="comment"># sklearn库有一个叫做 Imputer 库，专门用于处理缺失值问题</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">'titanic_train.csv'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>obj_df = df.select_dtypes(include=[<span class="string">'object'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.drop(obj_df.columns, axis=<span class="number">1</span>, inplace=<span class="keyword">True</span>)</span><br><span class="line"><span class="comment"># 我们必须删掉object类型的数据，Imputer才能处理，否则会报错</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">from</span> sklearn.preprocessing <span class="keyword">import</span> Imputer</span><br><span class="line"><span class="comment"># 拿到一个带有缺失值的DataFrame</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>impute = pd.DataFrame(Imputer().fit_transform(df))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>impute.columns = df.columns</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>impute.index = df.index</span><br></pre></td></tr></table></figure>
<h3 id="索引高级内容"><a href="#索引高级内容" class="headerlink" title="索引高级内容"></a>索引高级内容</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series(np.arange(<span class="number">5</span>), index=np.arange(<span class="number">5</span>)[::<span class="number">-1</span>], dtype=<span class="string">'int64'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line"><span class="number">4</span>    <span class="number">0</span></span><br><span class="line"><span class="number">3</span>    <span class="number">1</span></span><br><span class="line"><span class="number">2</span>    <span class="number">2</span></span><br><span class="line"><span class="number">1</span>    <span class="number">3</span></span><br><span class="line"><span class="number">0</span>    <span class="number">4</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 返回s集合中是否都在这个列表中</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.isin([<span class="number">1</span>,<span class="number">3</span>,<span class="number">4</span>])</span><br><span class="line"><span class="number">4</span>    <span class="keyword">False</span></span><br><span class="line"><span class="number">3</span>     <span class="keyword">True</span></span><br><span class="line"><span class="number">2</span>    <span class="keyword">False</span></span><br><span class="line"><span class="number">1</span>     <span class="keyword">True</span></span><br><span class="line"><span class="number">0</span>     <span class="keyword">True</span></span><br><span class="line">dtype: bool</span><br><span class="line"><span class="comment"># 取出对应的内容</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s[s.isin([<span class="number">1</span>,<span class="number">3</span>,<span class="number">4</span>])]</span><br><span class="line"><span class="number">3</span>    <span class="number">1</span></span><br><span class="line"><span class="number">1</span>    <span class="number">3</span></span><br><span class="line"><span class="number">0</span>    <span class="number">4</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dates = pd.date_range(<span class="string">'20171124'</span>, periods=<span class="number">8</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(np.random.randn(<span class="number">8</span>,<span class="number">4</span>), index=dates, columns=[<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'D'</span>])</span><br><span class="line"><span class="comment"># 将大于0的记录都变为8</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.where(df&lt;<span class="number">0</span>, <span class="number">8</span>)</span><br><span class="line">                   A         B         C         D</span><br><span class="line"><span class="number">2017</span><span class="number">-11</span><span class="number">-24</span> <span class="number">-0.876734</span>  <span class="number">8.000000</span>  <span class="number">8.000000</span> <span class="number">-2.213080</span></span><br><span class="line"><span class="number">2017</span><span class="number">-11</span><span class="number">-25</span> <span class="number">-1.192806</span>  <span class="number">8.000000</span> <span class="number">-1.032912</span> <span class="number">-0.500371</span></span><br><span class="line"><span class="number">2017</span><span class="number">-11</span><span class="number">-26</span> <span class="number">-0.425647</span> <span class="number">-0.347671</span> <span class="number">-0.976020</span> <span class="number">-0.150681</span></span><br><span class="line"><span class="number">2017</span><span class="number">-11</span><span class="number">-27</span> <span class="number">-1.279070</span>  <span class="number">8.000000</span>  <span class="number">8.000000</span>  <span class="number">8.000000</span></span><br><span class="line"><span class="number">2017</span><span class="number">-11</span><span class="number">-28</span> <span class="number">-0.362680</span> <span class="number">-0.095654</span>  <span class="number">8.000000</span>  <span class="number">8.000000</span></span><br><span class="line"><span class="number">2017</span><span class="number">-11</span><span class="number">-29</span>  <span class="number">8.000000</span>  <span class="number">8.000000</span> <span class="number">-0.810233</span> <span class="number">-0.044836</span></span><br><span class="line"><span class="number">2017</span><span class="number">-11</span><span class="number">-30</span>  <span class="number">8.000000</span> <span class="number">-0.036969</span>  <span class="number">8.000000</span>  <span class="number">8.000000</span></span><br><span class="line"><span class="number">2017</span><span class="number">-12</span><span class="number">-01</span> <span class="number">-0.587358</span>  <span class="number">8.000000</span> <span class="number">-1.363178</span>  <span class="number">8.000000</span></span><br><span class="line"><span class="comment"># query，组合复杂的条件</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(np.random.rand(<span class="number">10</span>,<span class="number">3</span>), columns=list(<span class="string">'abc'</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">          a         b         c</span><br><span class="line"><span class="number">0</span>  <span class="number">0.925144</span>  <span class="number">0.939164</span>  <span class="number">0.467199</span></span><br><span class="line"><span class="number">1</span>  <span class="number">0.032413</span>  <span class="number">0.865354</span>  <span class="number">0.318904</span></span><br><span class="line"><span class="number">2</span>  <span class="number">0.265597</span>  <span class="number">0.771220</span>  <span class="number">0.318450</span></span><br><span class="line"><span class="number">3</span>  <span class="number">0.643624</span>  <span class="number">0.630970</span>  <span class="number">0.739700</span></span><br><span class="line"><span class="number">4</span>  <span class="number">0.099581</span>  <span class="number">0.409716</span>  <span class="number">0.314810</span></span><br><span class="line"><span class="number">5</span>  <span class="number">0.224205</span>  <span class="number">0.340918</span>  <span class="number">0.380008</span></span><br><span class="line"><span class="number">6</span>  <span class="number">0.309103</span>  <span class="number">0.328867</span>  <span class="number">0.569452</span></span><br><span class="line"><span class="number">7</span>  <span class="number">0.761342</span>  <span class="number">0.545703</span>  <span class="number">0.758707</span></span><br><span class="line"><span class="number">8</span>  <span class="number">0.341552</span>  <span class="number">0.561309</span>  <span class="number">0.989554</span></span><br><span class="line"><span class="number">9</span>  <span class="number">0.797382</span>  <span class="number">0.973130</span>  <span class="number">0.129032</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.query(<span class="string">'a&lt;b &amp; b&lt;c'</span>)</span><br><span class="line">          a         b         c</span><br><span class="line"><span class="number">5</span>  <span class="number">0.224205</span>  <span class="number">0.340918</span>  <span class="number">0.380008</span></span><br><span class="line"><span class="number">6</span>  <span class="number">0.309103</span>  <span class="number">0.328867</span>  <span class="number">0.569452</span></span><br><span class="line"><span class="number">8</span>  <span class="number">0.341552</span>  <span class="number">0.561309</span>  <span class="number">0.989554</span></span><br></pre></td></tr></table></figure>
<h3 id="pandas绘图"><a href="#pandas绘图" class="headerlink" title="pandas绘图"></a>pandas绘图</h3><p>pandas可以直接绘制图形，这里不贴图形内容，只写代码，掌握方法即可<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Series结构画图</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series(np.random.randn(<span class="number">10</span>), index=np.arange(<span class="number">0</span>,<span class="number">100</span>,<span class="number">10</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line"><span class="number">0</span>    <span class="number">-0.265223</span></span><br><span class="line"><span class="number">10</span>   <span class="number">-0.193092</span></span><br><span class="line"><span class="number">20</span>   <span class="number">-0.929230</span></span><br><span class="line"><span class="number">30</span>   <span class="number">-0.079986</span></span><br><span class="line"><span class="number">40</span>    <span class="number">1.643548</span></span><br><span class="line"><span class="number">50</span>    <span class="number">0.344221</span></span><br><span class="line"><span class="number">60</span>    <span class="number">0.790363</span></span><br><span class="line"><span class="number">70</span>    <span class="number">2.599083</span></span><br><span class="line"><span class="number">80</span>    <span class="number">0.893276</span></span><br><span class="line"><span class="number">90</span>   <span class="number">-0.227786</span></span><br><span class="line">dtype: float64</span><br><span class="line"><span class="comment"># notebook使用魔法指令 %matplotlib inline</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.plot()</span><br><span class="line"></span><br><span class="line"><span class="comment"># DataFrame绘图</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(np.random.randn(<span class="number">10</span>, <span class="number">4</span>).cumsum(<span class="number">0</span>), </span><br><span class="line"><span class="meta">... </span>           index = np.arange(<span class="number">0</span>, <span class="number">100</span>, <span class="number">10</span>), </span><br><span class="line"><span class="meta">... </span>           columns = [<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'D'</span>])</span><br><span class="line">df.plot()</span><br><span class="line"><span class="comment"># 结合matplotlib</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fit,axes = plt.subplots(<span class="number">2</span>,<span class="number">1</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = pd.Series(np.random.rand(<span class="number">16</span>),index=list(<span class="string">'abcdefghijklmnop'</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data.plot(ax=axes[<span class="number">0</span>],kind=<span class="string">'bar'</span>)</span><br><span class="line">&lt;matplotlib.axes._subplots.AxesSubplot object at <span class="number">0x1180ee208</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data.plot(ax=axes[<span class="number">1</span>],kind=<span class="string">'barh'</span>)</span><br><span class="line">&lt;matplotlib.axes._subplots.AxesSubplot object at <span class="number">0x1a19dd16d8</span>&gt;</span><br><span class="line"><span class="comment"># 画柱状图</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(np.random.rand(<span class="number">6</span>,<span class="number">4</span>),</span><br><span class="line"><span class="meta">... </span>                 index = [<span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'three'</span>, <span class="string">'four'</span>, <span class="string">'five'</span>, <span class="string">'six'</span>],</span><br><span class="line"><span class="meta">... </span>                 columns = pd.Index([<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'D'</span>], name=<span class="string">'Genus'</span>))</span><br><span class="line"><span class="comment"># 使用kind制定类型</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.plot(kind=<span class="string">'bar'</span>)</span><br><span class="line">&lt;matplotlib.axes._subplots.AxesSubplot object at <span class="number">0x1a19f30278</span>&gt;</span><br><span class="line"><span class="comment"># 直方图</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>tips = pd.read_csv(<span class="string">'tips.csv'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>tips.total_bill.plot(kind=<span class="string">'hist'</span>, bins=<span class="number">50</span>)</span><br><span class="line"><span class="comment"># 散点图</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>macro = pd.read_csv(<span class="string">'macrodata.csv'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = macro[[<span class="string">'quarter'</span>, <span class="string">'realgdp'</span>, <span class="string">'realcons'</span>]]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data.plot.scatter(<span class="string">'quarter'</span>,<span class="string">'realgdp'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.scatter_matrix(data, color=<span class="string">'g'</span>, alpha=<span class="number">0.3</span>)</span><br></pre></td></tr></table></figure></p>
<h3 id="大数据处理技巧"><a href="#大数据处理技巧" class="headerlink" title="大数据处理技巧"></a>大数据处理技巧</h3><p>当我们的样本数据非常大的时候，我们直接将其放到内存可能会非常费内存，这里讨论一些可能的优化措施。如果数据非常大，内存怎么优化都存不下，就应该考虑其他方式了，这里提及的大数据处理技巧并非全能</p>
<ol>
<li>如何处理特大的数据<br>这里实验一个291M的数据</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line">g1.shape                        <span class="comment"># 可以看到这个数据集的列非常多，处理起来会非常慢</span></span><br><span class="line">g1.info(memory_usage=<span class="string">'deep'</span>)    <span class="comment"># 看一下数据详细情况</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过info函数我们看到元素类型，dtypes: float64(77), int64(6), object(78)</span></span><br><span class="line"><span class="comment"># 我们计算一下每个类型平均占用的内存大小</span></span><br><span class="line"><span class="keyword">for</span> dtype <span class="keyword">in</span> [<span class="string">'float64'</span>, <span class="string">'int64'</span>, <span class="string">'object'</span>]:</span><br><span class="line">    selected_dtype = g1.select_dtypes(include=[dtype])</span><br><span class="line">    mean_usage_b = selected_dtype.memory_usage(deep=<span class="keyword">True</span>).mean()</span><br><span class="line">    mean_usage_mb = mean_usage_b / <span class="number">1024</span> ** <span class="number">2</span></span><br><span class="line">    print(<span class="string">'mean memory usage: '</span>, dtype, mean_usage_mb)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过上面的程序，您看到了不同类型占用的平均大小了吧，我们现在先来优化整数</span></span><br><span class="line"><span class="comment"># 计算一下不同类型的整数能够表示的最大数</span></span><br><span class="line">int_types = [<span class="string">'uint8'</span>, <span class="string">'int8'</span>, <span class="string">'int16'</span>, <span class="string">'int32'</span>, <span class="string">'int64'</span>]</span><br><span class="line"><span class="keyword">for</span> it <span class="keyword">in</span> int_types:</span><br><span class="line">    <span class="keyword">print</span> (np.iinfo(it))</span><br><span class="line"><span class="comment"># 如果你能确定你的样本的数据在对应类型能够表示的范围，就可以转一个最小化内存的类型，为了便于计算，我们定义一个计算内存占用的函数</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">mem_usage</span><span class="params">(pandas_obj)</span>:</span></span><br><span class="line">    <span class="keyword">if</span> isinstance(pandas_obj, pd.DataFrame):</span><br><span class="line">        usage_b = pandas_obj.memory_usage(deep=<span class="keyword">True</span>).sum()</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        usage_b = pandas_obj.memory_usage(deep=<span class="keyword">True</span>)</span><br><span class="line">    usage_mb = usage_b / <span class="number">1024</span> ** <span class="number">2</span></span><br><span class="line">    <span class="keyword">return</span> <span class="string">'&#123;:03.2f&#125; MB'</span>.format(usage_mb)</span><br><span class="line"><span class="comment"># 下面我们将 int64（默认读取时的类型）进行向下类型转换，然后分别对比一下转换前后的内存占用</span></span><br><span class="line">g1_int = g1.select_dtypes(include=[<span class="string">'int64'</span>])</span><br><span class="line">coverted_int = g1_int.apply(pd.to_numeric, downcast=<span class="string">'unsigned'</span>) <span class="comment"># 将每个元素类型都进行向下类型转换</span></span><br><span class="line">print(mem_usage(g1_int))        <span class="comment"># 7.87 MB</span></span><br><span class="line">print(mem_usage(coverted_int))  <span class="comment"># 1.48 MB</span></span><br><span class="line"><span class="comment"># 以上的对比的方式会了么？对于float也类似，将float64转为float32，内存会省一半</span></span><br><span class="line">g1_float = g1.select_dtypes(include=[<span class="string">'float64'</span>])</span><br><span class="line">coverted_float = g1_float.apply(pd.to_numeric, downcast=<span class="string">'float'</span>)</span><br><span class="line">print(mem_usage(g1_float))</span><br><span class="line">print(mem_usage(coverted_float))</span><br><span class="line"><span class="comment"># 这里我们将原始数据集转化一下，查看整个数据集的内存占用情况</span></span><br><span class="line">optimized_g1 = g1.copy()</span><br><span class="line">optimized_g1[coverted_int.columns] = coverted_int</span><br><span class="line">optimized_g1[coverted_float.columns] = coverted_float</span><br><span class="line">print(mem_usage(g1))                <span class="comment"># 861.57 MB</span></span><br><span class="line">print(mem_usage(optimized_g1))      <span class="comment"># 804.69 MB</span></span><br><span class="line"><span class="comment"># 看着好像也没剩多少，这是因为占用内存最多的是object，下面看看怎么优化object内存</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 优化object类型，我们先看一下object类型都是些什么数据</span></span><br><span class="line">g1_obj = g1.select_dtypes(include=[<span class="string">'object'</span>]).copy()</span><br><span class="line">g1_obj.describe()       </span><br><span class="line"><span class="comment"># 查看结果关注 day_of_week这个列，其类型是object，通过count指标可以看到有171907个数据，查看unique指标可以看到自由7个值，这种数据就是非常好的优化点</span></span><br><span class="line"><span class="comment"># 对于这种只有少数不同值的参数，我们可以使用category类型去替换，这样所有数据就只会占用这7个不同category的内存</span></span><br><span class="line"><span class="comment"># 转换方式也是非常方便</span></span><br><span class="line">dow = g1_obj.day_of_week</span><br><span class="line">dow_cat = dow.astype(<span class="string">'category'</span>)</span><br><span class="line"><span class="comment"># 通过codes来看一下实际转换后的值</span></span><br><span class="line">dow_cat.cat.codes       <span class="comment"># 查看结果是否发现了什么了呢，之前171907个object类型的占用空间，现在只有7个占用空间，明白了么</span></span><br><span class="line"><span class="comment"># 我们再使用上面定义的查看内存占用情况的函数来直观的看一下到底剩了多少空间</span></span><br><span class="line">print(mem_usage(dow))       <span class="comment"># 9.84 MB</span></span><br><span class="line">print(mem_usage(dow_cat))   <span class="comment"># 0.16 MB</span></span><br><span class="line"><span class="comment"># 我们循环得来处理所有的列，检查重复值的比例，如果小于了0.5，我们就转换为category类型</span></span><br><span class="line">converted_obj = pd.DataFrame()</span><br><span class="line"><span class="keyword">for</span> col <span class="keyword">in</span> g1_obj.columns:</span><br><span class="line">    num_unique_values = len(g1_obj[col].unique())</span><br><span class="line">    num_total_values = len(g1_obj[col])</span><br><span class="line">    <span class="keyword">if</span> num_unique_values / num_total_values &lt; <span class="number">0.5</span>:</span><br><span class="line">        converted_obj.loc[:,col] = g1_obj[col].astype(<span class="string">'category'</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        converted_obj.loc[:,col] = g1_obj[col]</span><br><span class="line"><span class="comment"># 查看内存情况</span></span><br><span class="line">print(mem_usage(g1_obj))            <span class="comment"># 752.72 MB</span></span><br><span class="line">print(mem_usage(converted_obj))     <span class="comment"># 1.67 MB</span></span><br><span class="line"><span class="comment"># 现在这个优化效果是不是非常棒呢</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 时间类型的优化</span></span><br><span class="line"><span class="comment"># 如果我们的数据有时间类型，那么将其转换为int32类型会更省空间，现在这个数据集中有一个date列，自己试验一下吧</span></span><br></pre></td></tr></table></figure>
<h3 id="读取网络文件"><a href="#读取网络文件" class="headerlink" title="读取网络文件"></a>读取网络文件</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>url = <span class="string">'https://archive.ics.uci.edu/m1/machine-learning-databases/00383/risk_factors_cervical_cancer.csv'</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(url, na_values=<span class="string">"?"</span>)        </span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.head()</span><br><span class="line"><span class="meta">... </span>...</span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/pandas/" rel="tag"># pandas</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/09/15/1.html" rel="next" title="数据分析之numpy">
                <i class="fa fa-chevron-left"></i> 数据分析之numpy
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/09/18/1.html" rel="prev" title="数据分析之matplotlib">
                数据分析之matplotlib <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpg"
                alt="jackstraw" />
            
              <p class="site-author-name" itemprop="name">jackstraw</p>
              <p class="site-description motion-element" itemprop="description">人生的意义，不在于最终获得了什么，而在于曾经努力追求过什么</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">22</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">22</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/blog/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#pandas"><span class="nav-number">1.</span> <span class="nav-text">pandas</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#数据准备"><span class="nav-number">1.1.</span> <span class="nav-text">数据准备</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#基础"><span class="nav-number">1.2.</span> <span class="nav-text">基础</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#关于DataFrame的基本操作"><span class="nav-number">1.2.1.</span> <span class="nav-text">关于DataFrame的基本操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#关于Series的基本操作"><span class="nav-number">1.2.2.</span> <span class="nav-text">关于Series的基本操作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#进阶"><span class="nav-number">1.3.</span> <span class="nav-text">进阶</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#数据的筛选"><span class="nav-number">1.3.1.</span> <span class="nav-text">数据的筛选</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#groupby"><span class="nav-number">1.3.2.</span> <span class="nav-text">groupby</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数值运算"><span class="nav-number">1.3.3.</span> <span class="nav-text">数值运算</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#DataFrame与Series对象的操作"><span class="nav-number">1.3.4.</span> <span class="nav-text">DataFrame与Series对象的操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#关于pandas的选项设置"><span class="nav-number">1.3.5.</span> <span class="nav-text">关于pandas的选项设置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#pandas对时间的操作"><span class="nav-number">1.3.6.</span> <span class="nav-text">pandas对时间的操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字符串处理"><span class="nav-number">1.3.7.</span> <span class="nav-text">字符串处理</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#高级主题"><span class="nav-number">1.4.</span> <span class="nav-text">高级主题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#数据透视表"><span class="nav-number">1.4.1.</span> <span class="nav-text">数据透视表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#排序操作"><span class="nav-number">1.4.2.</span> <span class="nav-text">排序操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#去重"><span class="nav-number">1.4.3.</span> <span class="nav-text">去重</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#自定义处理方法"><span class="nav-number">1.4.4.</span> <span class="nav-text">自定义处理方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用assign新增一列，可以使用其他列进行计算"><span class="nav-number">1.4.5.</span> <span class="nav-text">使用assign新增一列，可以使用其他列进行计算</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#其他处理"><span class="nav-number">1.4.6.</span> <span class="nav-text">其他处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#索引高级内容"><span class="nav-number">1.4.7.</span> <span class="nav-text">索引高级内容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#pandas绘图"><span class="nav-number">1.4.8.</span> <span class="nav-text">pandas绘图</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#大数据处理技巧"><span class="nav-number">1.4.9.</span> <span class="nav-text">大数据处理技巧</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#读取网络文件"><span class="nav-number">1.4.10.</span> <span class="nav-text">读取网络文件</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">jackstraw</span>

  
</div>









        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>访问人数
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>总访问量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: true,
        notify: false,
        appId: 'lQM75w94ggNR0TjX61NLerrg-gzGzoHsz',
        appKey: 'I2wtQ2rd9KtoJmcEOiYG9zqT',
        placeholder: '如需帮助，请留下邮箱',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
    var infoEle = document.querySelector('#comments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0){
      infoEle.childNodes.forEach(function(item) {
        item.parentNode.removeChild(item);
      });
    }
  </script>



  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("lQM75w94ggNR0TjX61NLerrg-gzGzoHsz", "I2wtQ2rd9KtoJmcEOiYG9zqT");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
